Sideclef Posted August 7, 2008 Share Posted August 7, 2008 Well I am completely new to php. I have this script I have been working on for a few days now everything is working except the 2 update statements. I am sure that my script is pretty sloppy as well I apologize for that but as I have stated before I am quite new to this. Any ways the purpose of this script is to take money from one player and give to another. It is not based in sessions yes I know that's not good practice I am in the process of changing that now. Here's the script hope someone can be of some help. Thanks. $choice=strip_tags($_POST['choice']); if ($choice==2){ //getting the random player $sql = mysql_query ("SELECT * FROM `users` ORDER BY RAND() LIMIT 0,1") or die (mysql_error ()); $res = mysql_fetch_assoc ($sql); $randplayer = $res['username']; $geld =$res['geld']; //taking money from the random player $percent = rand(10,25); $takenGeld = round($geld * ($percent / 100)); $picker =mysql_query("SELECT geld FROM users WHERE username=$cookieusername"); $test=round($geld / $percent); $test1=$geld-$takenGled; $test2=$picker+$takenGeld; mysql_query("UPDATE `users` SET `geld`='$test1' WHERE `username`='$randplayer'"); mysql_query("UPDATE `users` SET `geld`='$test2' WHERE `username`='$cookieusername'"); } echo"You took $takenGeld from $randplayer This is a test amout left $test1 and amout took $test2'"; ?> Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/ Share on other sites More sharing options...
Stooney Posted August 7, 2008 Share Posted August 7, 2008 Change these lines to this and see what it says: mysql_query("UPDATE `users` SET `geld`='$test1' WHERE `username`='$randplayer'") or die(mysql_error()); mysql_query("UPDATE `users` SET `geld`='$test2' WHERE `username`='$cookieusername'") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/#findComment-611009 Share on other sites More sharing options...
Sideclef Posted August 7, 2008 Author Share Posted August 7, 2008 No change it is posting the amount left for the random player for the user instead of adding it to the users original amount Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/#findComment-611026 Share on other sites More sharing options...
Stooney Posted August 7, 2008 Share Posted August 7, 2008 Try outputting the query string to see if it's what you expect. So... echo "UPDATE `users` SET `geld`='$test1' WHERE `username`='$randplayer'"; Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/#findComment-611031 Share on other sites More sharing options...
Sideclef Posted August 8, 2008 Author Share Posted August 8, 2008 UPDATE `users` SET `geld`='1364' WHERE `username`='test1' UPDATE `users` SET `geld`='136' WHERE `username`='Sideclef' You took 136 from test1 this is a test amount left 1364 and amount took 136' This was the out put everything is working fine but its posting 1500 (the randplayers original amount) to the user that took it instead of the 136. Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/#findComment-611520 Share on other sites More sharing options...
Sideclef Posted August 9, 2008 Author Share Posted August 9, 2008 anyone? Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/#findComment-612510 Share on other sites More sharing options...
Barand Posted August 9, 2008 Share Posted August 9, 2008 try <?php $choice=strip_tags($_POST['choice']); if ($choice==2){ //getting the random player $sql = mysql_query ("SELECT * FROM `users` ORDER BY RAND() LIMIT 0,1") or die (mysql_error ()); $res = mysql_fetch_assoc ($sql); $randplayer = $res['username']; $geld = $res['geld']; //taking money from the random player $percent = rand(10,25); $takenGeld = round($geld * ($percent / 100)); mysql_query("UPDATE `users` SET geld=geld-$takenGeld WHERE `username`='$randplayer'"); mysql_query("UPDATE `users` SET geld=geld+$takenGeld WHERE `username`='$cookieusername'"); } ?> Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/#findComment-612628 Share on other sites More sharing options...
Sideclef Posted August 10, 2008 Author Share Posted August 10, 2008 Thank you so much it works just fine. I had tried it that way when I first started having problems with it but mine didn't work for some reason.Thanks again much appreciated. Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/#findComment-612861 Share on other sites More sharing options...
Sideclef Posted August 10, 2008 Author Share Posted August 10, 2008 Well let me take that back the option it self works just fine now throughout the whole site every query to geld is returning 0 but the data base is at the correct # I am quite confused on why it would be happening since it worked fine before I added this script. Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/#findComment-612862 Share on other sites More sharing options...
Sideclef Posted August 10, 2008 Author Share Posted August 10, 2008 Fixed thanks again Link to comment https://forums.phpfreaks.com/topic/118676-cant-update-query-properly/#findComment-612873 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.