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!
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?
// 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.