Question:
Combine these 2 queries into ?
anonymous
2008-10-27 10:25:27 UTC
mysql_query("update users set money=money-'$amount' where id='$users[id]'") or die (mysql_error());

mysql_query("update users set money=money+'$amount' where id='$recieve_id[id]'") or die (mysql_error());
Three answers:
TravisO
2008-10-27 10:31:05 UTC
You can't, these are two unrelated queriers that cannot be merged into a single query.



Also your SQL contains a HUGE sql injection mistake, which will allow any hacker to abuse your entire database. DO NOT put form input into your SQL without doing a mysql_real_escape_string() on the data, otherwise they could enter '; DROP TABLE users; -- and cause serious damage, as well as do all kinds of other things.



http://us.php.net/mysql_real_escape_string



As far as speeding this up, as I'm guessing you have a list of IDs you want to improve is by writing a Store Procedure in MySQL that you give a list of IDs to, and it runs all the updates for you.



http://www.mysql.com/news-and-events/newsletter/2004-01/a0000000297.html

http://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspx



Simply provide a comma delimited list of the IDs, then in your Stored Procedure split the parameter by comma and perform a loop. This will be significantly faster than doing the loop in PHP.
Chris C
2008-10-27 10:58:17 UTC
Proper SQL to combine these queries.

And if you have properly sanitized the data coming in to this function I wouldn't worry about SQL injections. So, don't take data that is directly sent on the URL, as though it shouldn't be validated.



UPDATE users SET money = money + '$amount' WHERE id = $users[id] OR id = $receive_id[id]
strenge
2016-10-20 04:27:44 UTC
Marlene Asmerelda, Marlene Barbara, Marlene Cordelia, Marlene Desdemona, Marlene Fawna, Marlene Gwen, Marlene Helen, Marlene Ivanna, Marlene Jessica, Marlene Karoline, Marlene Louise, Marlene Martina, Marlene Natalie, Marlene Paris, Marlene Rina, Marlene Queen, Marlene Rachael, Marlene Selena, and Marlene Tracey. Janine Abbigail, Janine Britney, Janine Clotilda, Janine Diana, Janine Eleanor, Janine Fiona, Janine Gallerina, Janine Hilary, Janine Isabella, Janine Julianna, Janine Kathy, Janine Lazena, Janine Maggie, Janine Noella, Janine Opal, Janine Polina, Janine Rosa, Janine Sasha, and Janine Tara.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...