Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I increment a value with mysql update query (php)

I have a query that looks like this:

$sql = "UPDATE tbl SET amt_field='amt_field+1' WHERE username='" .mysql_real_escape_string($_SESSION['username']). "'";
        mysql_select_db('db',$con);
        mysql_query($sql,$con);

I want to increment the value as easily as possible.

I have tried:

"UPDATE tbl SET amt_field='amt_field+1' WHERE

"UPDATE tbl SET amt_field='amt_field' + 1 WHERE

"UPDATE tbl SET amt_field='amt_field++' WHERE

I don't get error messages, but the value in my db does not increase either.

like image 266
ganjan Avatar asked Feb 26 '23 14:02

ganjan


1 Answers

UPDATE tbl SET amt_field = amt_field + 1 WHERE ...

If you use the single quotes ', you're telling the enclosed value to be interpreted as a string You were probably thinking about the tick marks. This is also valid:

UPDATE tbl SET `amt_field` = `amt_field` + 1 WHERE ...

This must be used when the column (or table etc.) has a reserved name.

like image 69
Artefacto Avatar answered Mar 08 '23 05:03

Artefacto