Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Having problem my mysql update statement

    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.
    PHP Code:
    $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'"


  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Where are you selecting the new value after you update it?

  • #3
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    See inline comments:
    PHP Code:
    $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
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #4
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Fixed the misspell and added this to the code
    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

  • #5
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You need to quote your strings in your query. '$username'

  • #6
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok I have done that now I am getting this Fatal error: Unsupported operand types on line 53
    Code:
    //line 53
    $test2=$picker+$takenGeld;

  • #7
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Read dumpfi's code comment he posted about $picker.

  • #8
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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
    Code:
    $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.

  • #9
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    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:
    PHP Code:
    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
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • Users who have thanked dumpfi for this post:

    Why_me (08-09-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •