A
AngelMare
Guest
I asked this earlier and didn't get an answer I knew how to implement, so I'll add a bit more detail here.
How can I limit a row in a table to not go above 100 and not below 0, such as in percents, when that row is updated, and if the value ends up above 100 it will end up as 100, and if it goes below 0, it will end up as 0?
Here is the code that I'm using as an example. I want to add 5 points to "mood" but that if adding that would cause it to go over 100, that the table would be updated with 100.
<?php
// Get horse's record from the "horses" table and update the mood
$result = mysql_query("UPDATE horses SET mood = mood + 5 WHERE id='$id'")
or die(mysql_error());
$result = mysql_query("SELECT * FROM horses WHERE id='$id'")
or die(mysql_error());
// get the entry from the result
$row = mysql_fetch_array( $result );
echo ("<br>Horse has gained 5 mood points. <a href=horse.php?id=$id>Return to horse.</a>");
?>
I am also wondering how I could use this same idea in a money script so that if someone wants to buy something but don't have enough money, that the script would not let them do it, rather than either going into the negative numbers (as it is doing now) or setting it to 0.
Any help would be greatly appreciated!
How can I limit a row in a table to not go above 100 and not below 0, such as in percents, when that row is updated, and if the value ends up above 100 it will end up as 100, and if it goes below 0, it will end up as 0?
Here is the code that I'm using as an example. I want to add 5 points to "mood" but that if adding that would cause it to go over 100, that the table would be updated with 100.
<?php
// Get horse's record from the "horses" table and update the mood
$result = mysql_query("UPDATE horses SET mood = mood + 5 WHERE id='$id'")
or die(mysql_error());
$result = mysql_query("SELECT * FROM horses WHERE id='$id'")
or die(mysql_error());
// get the entry from the result
$row = mysql_fetch_array( $result );
echo ("<br>Horse has gained 5 mood points. <a href=horse.php?id=$id>Return to horse.</a>");
?>
I am also wondering how I could use this same idea in a money script so that if someone wants to buy something but don't have enough money, that the script would not let them do it, rather than either going into the negative numbers (as it is doing now) or setting it to 0.
Any help would be greatly appreciated!