Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Syntax Error when using primary key with WHERE

Tags:

sql

php

I am trying to use UPDATE with my MySQL-Database. I use the following SQL code:

$sql =  "UPDATE ToDo 
    SET Checked = -1
    WHERE Index = 1";

When I use this code i get the following error message: "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 'Index = 1' at line 3"

But when I use

$sql =  "UPDATE ToDo 
    SET Checked = -1
    WHERE Text = 'asdf'";

Everything works.

My database has one table named "ToDo" with 3 collumns: Index(int, primary key, auto_increment), Checked(bool) and Text(text).

Can't you "WHERE" a primary key or did i forget something else?

Hope you can help me.

like image 759
Martin Avatar asked Feb 27 '26 05:02

Martin


1 Answers

Try adding the backticks:

UPDATE ToDo 
    SET `Checked` = -1
    WHERE `Index` = 1";

Index is a reserved word :

http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

like image 191
Sabeen Malik Avatar answered Mar 01 '26 19:03

Sabeen Malik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!