How to increase value with mysql update request (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 increase the value as much as possible.

I 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 do not receive error messages, but the value in my db also does not increase.

+4
source share
2 answers
 UPDATE tbl SET amt_field = amt_field + 1 WHERE ... 

If you use single quotes ' , you say that the nested value is interpreted as a string. You probably thought about the grades. This is also true:

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

This should be used when a column (or table, etc.) has a reserved name.

+8
source

Hi, you have initiated a new session. Below worked fine for me.

  public static function insert_search($pdo) { @session_start(); $ip = $_SERVER['REMOTE_ADDR']; $username = $_SESSION['username']; //$username = self::username(); $date = date('Ym-d'); //Adding the total searches for the logged in user $query = $pdo->query("UPDATE `users` SET `total_searches` = `total_searches` +1 WHERE username = '$username'"); } /* What you could potentially do is the following. Make sure if you're doing it the procedural way you put @session_start(); at the top of the page */ @session_start(); $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); 
0
source

All Articles