Trouble Updating with a While loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Trouble Updating with a While loop

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Trouble Updating with a While loop

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Re: Trouble Updating with a While loop

Post by carleihar »

Thank you! It works now!
Post Reply