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='a开发者_开发技巧mt_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.
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.
Hello did you initialize a new session. Below worked perfectly for me.
public static function insert_search($pdo)
{
@session_start();
$ip = $_SERVER['REMOTE_ADDR'];
$username = $_SESSION['username'];
//$username = self::username();
$date = date('Y-m-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);
精彩评论