Jump to content

Cant Update query properly


Sideclef

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.