I'm trying to figure out what the best way of incrementing a value in table would be. I am trying to update the total value of an existing field in a mysql db.
I'd also like to be able to do it in one query rather than having 2 do a SELECT query and then an UPDATE query.
I'd love to be able to run a query like this:
mysql_query("UPDATE ans SET value=value+(value to be added);
The best solution that I have come up with is something such as this:
$hostname = "#####";
$username = "#####";
$password = "#####";
$dbname = "#####";
$add=3;//this is what will be added
$querya = "SELECT value FROM table2update WHERE id=2";
$result = MYSQL_QUERY($querya);
$value=mysql_fetch_object($result);
$newvalue = $value+ @;
$queryb = "UPDATE table2update SET $value = '$newvalue'WHERE id=2";
$exec = MYSQL_QUERY($queryb);
Surely this is not the best way to do this.
Help is greatly appreciated.
Bryan