Christopher
Christopher

Reputation: 267

Use Primary Key to reference row for UPDATE

I have a MySQL database which I'm trying to do an update or delete command with:

$query = "UPDATE db SET this='$_POST[f_that]',this2='$_POST[f_that2]' WHERE index='$_POST[f_index]'";

This returns a syntax error every time, saying the problem is near index='#'. The information comes from a table, which has input fields inline with the values set to the database (inline editing). The form_index is output in this fashion. Inside my database, the index is the primary key and auto incrementing.

I've print_r() the $_POST and the $query to make sure that the form_index is a number (and the right number at that), which it is.

Is there some rule that I can't reference purely on the primary key?

Upvotes: 2

Views: 125

Answers (3)

Amir
Amir

Reputation: 4111

index is reserved word, you should change it or use back stick like below

`index`

Upvotes: 0

Sid
Sid

Reputation: 560

Change column name for index. It is treating as a keyword in your query.

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

index is reserved word in mysql, you can't directly use it as column name, you have to use ` around column name for using these kind of reserved words.

$query = "UPDATE db SET `this`='$_POST[f_that]',`this2`='$_POST[f_that2]' 
WHERE `index`='$_POST[f_index]'";

Upvotes: 3

Related Questions