PHP - MySQL - Update current record by adding an integer?

  • Thread starter Thread starter AngelMare
  • Start date Start date
A

AngelMare

Guest
How do I update a record by adding an integer to the current integer, no matter what the current integer is?
For example, I have the current "mood" row in my database table set to "100" and I want to add 5, but allow 5 to be added to the "mood" integer even if it is a number other than 100. In this example, I want the "mood" row to end up being 105, but again I want it not to depend on the 100.
Any help would be appreciated.
 
UPDATE table SET mood = mood + 5 WHERE id =12345678

Obviously replacing "table" and "id" with the relevant names from your DB schema.
 
well..you can make a SELECT query..and retrieve the number, in a variable, for example, $mood

then you can add 5 to the $mood variable, whatever value it contains

$mood += 5;

and then use an UPDATE query and put the $mood back in the database

example:

$query = mysql_query('SELECT `mood` FROM <table_name>;');

$results = mysql_fetch_assoc($query);

$mood = $results['mood'];

$mood += 5;

and then make a UPDATE query ;)

hope this helps
 
well..you can make a SELECT query..and retrieve the number, in a variable, for example, $mood

then you can add 5 to the $mood variable, whatever value it contains

$mood += 5;

and then use an UPDATE query and put the $mood back in the database

example:

$query = mysql_query('SELECT `mood` FROM <table_name>;');

$results = mysql_fetch_assoc($query);

$mood = $results['mood'];

$mood += 5;

and then make a UPDATE query ;)

hope this helps
 
Back
Top