0

I have a MySQL query I'm running. I want to add 1 to a field called articleswritten.

I get 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 ''users' SET articleswritten = articleswritten + 1 WHERE id = '1'' at line 1

Code:

$sql = "UPDATE 'users' SET articleswritten = articleswritten + 1 WHERE `id` = '$userid'";
$result = mysql_query($sql) or die(mysql_error());

I can't find an issue. Am I blind?

Any help would be appreciated.

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
KriiV
  • 1,882
  • 4
  • 25
  • 43

2 Answers2

0

This should either be

UPDATE `users`

Or just

UPDATE users

The single quotes make the table name invalid. Everything else in the query is okay.


However, your query is vulnerable to injection. Instead of using ext/mysql, you should use properly parameterized queries with PDO or mysqli

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

You don't need to single quote the table name here. This should do

$sql = "UPDATE users SET articleswritten = articleswritten + 1 WHERE id = '$userid'";
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95