0

I have this part of code:

$result = mysql_query("SELECT * FROM posts WHERE id = '$id'") or die (mysql_error());
$rec = mysql_fetch_array($result) or die (mysql_error());
$like = $rec['like'];
$like += 1;
mysql_query("UPDATE posts SET like = '$like' WHERE id = '$id'") or die (mysql_error());

and returning this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like = '1' WHERE id = '43'' at line 1"

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
fdisotto
  • 95
  • 2
  • 7
  • PHP's `ext/mysql` (the `mysql_*` family) is [deprecated](http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated). Please use [something else](http://www.php.net/manual/en/mysqlinfo.api.choosing.php) instead. – pilcrow Jul 02 '12 at 15:33

4 Answers4

4

LIKE is a reserved MySQL keyword. Quote your column names to avoid mistaking them for keywords:

... SET `like` = ...
deceze
  • 510,633
  • 85
  • 743
  • 889
1

You are using a reserved keyword LIKE

try it like this:

"UPDATE `posts` SET `like` = '$like' WHERE `id` = '$id'"
NDM
  • 6,731
  • 3
  • 39
  • 52
0

Your problem is that like is a mysql keyword, either change the column name in your database or surround with backticks.

Also dont use mysql_ functions use mysqli or PDO as what you have is not safe and outmoded

allen213
  • 2,267
  • 2
  • 15
  • 21
0

"like" is a reserved word, it is not recommended to use it to name columns. If it is not possible for you to rename the column, modify your UPDATE statement by escaping it with backticks :

`like`
Community
  • 1
  • 1
Jérôme
  • 2,070
  • 15
  • 21