...

View Full Version : Having problem my mysql update statement



Why_me
08-07-2008, 08:31 PM
I am having problems with an update statement posting the right numbers.When it updates it don't return the new value it is returning the old value.I am very new to php and I am sure you can tell cause this is probably a pretty sloppy script. I have tried this in many different ways and I just cant get it to work. It is probably a very simple mistake but being so new to php I cant seem to point it out.


$choice=strip_tags($_POST['choice']);
if ($choice==2){
//getting the random player
$sql = mysql_query ("SELECT * FROM `users` ORDER BY RAND() LIMIT 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=$username");
$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=$username");

}


echo"You took $takenGeld from $randplayer
This is a test amount left $test1 and amount took $test2'";

Brandoe85
08-07-2008, 09:33 PM
Where are you selecting the new value after you update it?

dumpfi
08-07-2008, 09:36 PM
See inline comments:

$choice=strip_tags($_POST['choice']);
if ($choice==2){
//getting the random player
$sql = mysql_query ("SELECT * FROM `users` ORDER BY RAND() LIMIT 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=$username");
$test=round($geld / $percent);
$test1=$geld-$takenGled; // <-- wrong spelling of $takenGeld
$test2=$picker+$takenGeld; // <-- $picker is a mysql resource, you must fetch the row out of it and use the value from index 'geld'

mysql_query("UPDATE users SET geld=$test1 WHERE username=$randplayer");
mysql_query("UPDATE users SET geld= $test2 WHERE username=$username");

}
echo"You took $takenGeld from $randplayer
This is a test amount left $test1 and amount took $test2'";dumpfi

Why_me
08-08-2008, 02:24 PM
Fixed the misspell and added this to the code

$user = mysql_query ("SELECT geld FROM `users` WHERE username=$username") or die (mysql_error ());
$picker = mysql_fetch_assoc ($user);
now when I run this script it come up with

Unknown column 'Sideclef' in 'where clause'
Sideclef being the value of $username

Brandoe85
08-08-2008, 02:40 PM
You need to quote your strings in your query. '$username'

Why_me
08-08-2008, 02:58 PM
ok I have done that now I am getting this Fatal error: Unsupported operand types on line 53

//line 53
$test2=$picker+$takenGeld;

Brandoe85
08-08-2008, 03:48 PM
Read dumpfi's code comment he posted about $picker.

Why_me
08-08-2008, 05:40 PM
I have gotten quite confused in all theses changes so I have decided just to re post the script that I have edited I believe that I have made the changes that were suggested to me I am still having problems with this

$choice=strip_tags($_POST['choice']);
if ($choice==2){
//getting the random player

$rand = "SELECT * FROM users ORDER BY RAND() LIMIT 1" or die (mysql_error ());
$resultuserlist = mysql_query($rand);
while ($row = mysql_fetch_assoc($resultuserlist)) {
$geld = $row['geld'];
$randplayer=$row['username'];

}


$percent = rand(10,25);
$takenGeld = round($geld * ($percent / 100));
$userlist = "SELECT * FROM users WHERE username='$username'";
$resultuserlist = mysql_query($userlist);
while ($row = mysql_fetch_assoc($resultuserlist)) {
$usergeld = $row[geld];

}
$test=round($geld / $percent);
$test1=$geld-$takenGeld;
$test2=$usergeld+$takenGeld;

mysql_query("UPDATE users SET geld=$test1 WHERE username=$randplayer");
mysql_query("UPDATE users SET geld= $test2 WHERE username=$username");

}
echo"UPDATE users SET geld=$test1 WHERE username=$randplayer";
echo"UPDATE users SET geld= $test2 WHERE username=$username";
echo"You took $takenGeld from $randplayer";
echo"This is a test $randplayer amount left $test1 and amount total for $username $test2";




this is the output of the echo's
UPDATE users SET geld=313280 WHERE username=Outta
UPDATE users SET geld= 52057 WHERE username=Sideclef
You took 42720 from Outta
This is a test Outta amount left 313280 and total amount for Sideclef 52057
They out put the correct values in the update statements it just not posting to the data base. The funny thing is there is a footer included at the bottom of the page that it does post to but it post $user money to be the amount of $randplayer money before it is took from them which is really odd because the footers data comes form the same table that isn't updating.

dumpfi
08-09-2008, 10:19 AM
Within your update queries you should put the user names in quotes. To detect invalid SQL queries like these more easily you should do something like this:
define('DEBUG', 'screen'); // set to 'log' when the code goes live
function my_query($sql)
{
$r = mysql_query($sql);
if(!$r)
{
switch(DEBUG)
{
case 'screen':
die(mysql_error());
break;
case 'log':
// log the error into a file..
break;
default:
die('internal error'); // some error to show the user that something went wrong
}
}
return $r;
}

// and then you should use my_query(..SQL..) instead of mysql_query(..SQL..)dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum