I have a game website and I want to empty everyones garage (delete all their cars) but i want them to recieve the money back that the car is worth. How would i do it? Cars are in a table named 'garage' and the money is in the 'users' table Cheers
Table Name: garage Table garage contains: id, owner, car_name, worth I want the 'worth' to go to the 'owners' money so it would be 'update users set money =XXXXX' Cheers
How many records are you talking approx. a simple solution if not many records something like $query ="SELECT * FROM garage"; $result =mysql_query($query); while($row =mysql_fetch_assoc($result)){ $query2 ="UPDATE users SET money=money + $row['worth'] WHERE user_id='".$row['owner']."'"; if(mysql_query($query2)){ $d_query ="DELETE * FROM garage WHERE id='".$row['id']."'"; mysql_query($d_query); } } PHP: you need to format the sql a bit to suit. If you have an excessive amount another option would be to get all the distinct user from garge and sum the worth of the cars in one sql then update the users table
You could actually do this in two mySQL commands, without PHP loops. UPDATE users, garage SET users.money = users.money + garage.worth WHERE users.user_id = garage.owner then run: delete from garage I just tested this out to make sure it works. You will need to fix the tables and column fields for your needs.
Tried that and got Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/a/m/a/amafiali/html/clear_garage.php on line 9 line 9 is this... $query2 ="UPDATE users SET money=money + $row['worth'] WHERE user_id='".$row['owner']."'"; PHP: O and i forgot to mention it is a LOT of records
The code that I posted will work for any amount of records, large and small. Since the only php required is just to run the queries. However, this can be done through something like phpmyadmin. Here is a fix for BIG G's code $query2 ="UPDATE users SET money=money + '".$row['worth']."' WHERE user_id='".$row['owner']."'"; PHP:
Works but loading takes too long and it isnt my server SOmebody elses script works but just didnt refund them, it just deleted the cars Here is what i have <?php include_once"includes/db_connect.php"; include_once"includes/functions.php"; if (strip_tags($_POST['submit'])){ $query ="SELECT * FROM garage"; $result =mysql_query($query); while($row =mysql_fetch_assoc($result)){ $query2 ="UPDATE users SET money=money + '".$row['worth']."' WHERE username='".$row['owner']."'"; if(mysql_query($query2)){ $d_query ="DELETE * FROM garage WHERE id='".$row['id']."'"; mysql_query($d_query); } mysql_query("INSERT INTO `topics` (`id`, `username`, `title`, `topictext`, `forum`, `locked`, `sticky`, `lastreply`,`made`,`crew`) VALUES ('', 'CuBz', 'Garages Cleared', 'All garages have been cleared and you have recieved your money for each car', 'main', '0', '1', '999999999999','$date','$fetch->crew');") or die (mysql_error()); echo"All Users Refunded"; } } ?> PHP: <center><br><Br> <form method="post" name="form8" action=""> <table height=100 width="600" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="#000000"><tr><td align=center background=includes/grad.jpg height=30> Empty Garages</td></tr><tr> <td align=center>Clear Garages and give everyone their money for the cars</td></tr> <td align=center><input type="submit" value="submit" name="submit" class="custombutton"></td></tr> </form></table> HTML: