Page 1 of 1

Trouble Updating with a While loop

Posted: Fri Feb 05, 2010 9:45 am
by carleihar
I'm having a problem with my code below. It's a small code to be able to "feed" your horse thats in a database on a scale of 0-10. I want each horse to have their individual hunger, but for some reason it's updating all the user's horses with the same hunger number. Any help?

Code: Select all

<?php
 
 
include('includes/header01.html');
 
 
$q = "SELECT horse_name, horse_id, date_fed, hunger FROM hunger WHERE user_name='{$_COOKIE['username']}'";           
$r = @mysqli_query ($dbc, $q); // Run the query.
 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 
$date2=$row['date_fed'];
$horse_id=$row['horse_id'];
$horse_name=$row['horse_name'];
$hunger=$row['hunger'];
 
        
        
echo '<table> <tr>
               <td align="left">' . $horse_name . '</td>
               <td align="left">' . $hunger . '</td>
               <td align="left">' . $days . '</td></table>';
}              
               
              
 
        echo '<form action="" method="POST">';  
        echo '<input name="submit" type="submit" value="Feed Horses"><br />';  
        echo '</form>';  
               
               
   if(isset($_POST)){
       
       $q = "SELECT hunger FROM hunger WHERE user_name='{$_COOKIE['username']}'";           
        $r = @mysqli_query ($dbc, $q); // Run the query.
       while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
            
            $hunger = $row['hunger'];
       
       
       
       $hunger = $hunger + 1;
       if ($hunger > 10) {
       $hunger = 10;
       }
       if ($hunger < 0) {
       $hunger = 0;
       }
       
        $t = "UPDATE hunger SET date_fed=NOW(), hunger='$hunger' WHERE user_name='{$_COOKIE['username']}'";
       $p = @mysqli_query($dbc, $t);
       
       }
    } 
    
include('includes/footer01.html');
?>

P.S. My header include has my sqli_connection.

Re: Trouble Updating with a While loop

Posted: Fri Feb 05, 2010 10:40 am
by AbraCadaver
You need a way to distinguish each individual horse:

Code: Select all

        // select horse_id
        $q = "SELECT horse_id, hunger FROM hunger WHERE user_name='{$_COOKIE['username']}'";          
        $r = @mysqli_query ($dbc, $q); // Run the query.
 
        while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
           
            // assign horse_id
            $horse_id = $row['horse_id'];
            $hunger = $row['hunger'];
 
            $hunger = $hunger + 1;
 
            if ($hunger > 10) {
                $hunger = 10;
            }
            if ($hunger < 0) {
                $hunger = 0;
            }
           // add horse_id condition
           $t = "UPDATE hunger SET date_fed=NOW(), hunger='$hunger' WHERE user_name='{$_COOKIE['username']}' AND horse_id='{$horse_id}'";
           $p = @mysqli_query($dbc, $t);       
       }
    }
Actually though, you have already done the query at the top of the script to display the horses so no need to query again, just change the original $row to $row[] and then loop through it to do your update.

Re: Trouble Updating with a While loop

Posted: Fri Feb 05, 2010 1:10 pm
by carleihar
Thank you! It works now!