elrado88
elrado88

Reputation: 75

PDO Update Statement not working

I am trying to update a simple query and I keep getting the following error message...

SQLSTATE[42000]: Syntax error or access violation: 1064 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 ':cat_name, menu_category_description = :cat_desc WHERE id = :id' at line 1

The query I am using looks perfectly valid. I don't know why it keeps showing that error message.

Below is my query I am using.

$query = "UPDATE menu_categories SET menu_category_name = :cat_name, menu_category_description = :cat_desc WHERE id = :id ";

$stmt = $db->query($query);
$stmt->execute([":cat_name" =>$category_name, ":cat_desc" => $category_description, ":id" => $id ]);

Upvotes: 0

Views: 56

Answers (2)

user1149244
user1149244

Reputation: 755

See if this works for you,

$query = "UPDATE menu_categories SET menu_category_name = :cat_name, menu_category_description = :cat_desc WHERE id = :id ";

$stmt = $db->prepare($query);
$stmt->bindParam(':cat_name', $category_name);
$stmt->bindParam(':cat_desc ', $category_description);
$stmt->bindParam(':id', $id);
$stmt->execute();

Also, where are you defining values for your $category_name, $category_description, $id? Make sure they are not empty.

Here's an example of Updating PDO

$pdo = Database::getInstance();
$stmt = $pdo->db->prepare("UPDATE people SET reset='1', active=:acTive WHERE user_id=:id limit 1");           
$stmt->bindParam(':acTive', $_POST['active_key']);
$stmt->bindParam(':id', $_POST['id']);
$stmt->execute();

Hope it helps

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You need to first prepare your query: (you're querying instead of preparing)

change this line:

$stmt = $db->query($query);

to:

$stmt = $db->prepare($query);

then change this line

$stmt->execute([":cat_name" =>$category_name, ":cat_desc" => $category_description, ":id" => $id ]);

to (and remove the square brackets)

$stmt->execute(":cat_name" =>$category_name, ":cat_desc" => $category_description, ":id" => $id);

Upvotes: 1

Related Questions