user1782964 February 2016

PHP Update Data in MySQL using PDO

I'm trying to Update Data in MySQL using PDO.

I have set up the code below but get the error

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 '(h1, text) values ('Blah blah' at line 1

if (isset($_POST['Submit']))://if admin wants to edit category

$h1 = $_POST['h1'];
$text = $_POST['text'];

$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "dbname";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception

// Prepare statement
$stmt = $conn->prepare('UPDATE sections (h1, text) values (:h1, :text) WHERE id=1');
$stmt->bindParam(':h1', $h1);   
$stmt->bindParam(':text', $text);   
// execute the query

// echo a message to say the UPDATE succeeded
echo $stmt->rowCount() . " records UPDATED successfully";
catch(PDOException $e)
echo $sql . "<br>" . $e->getMessage();

$conn = null;


Pathik Vejani February 2016

Update syntax should be:

UPDATE table_name SET `field1` = new-value1, `field2` = new-value2.

Your query should be:

UPDATE sections SET `h1` = :h1, `text` = :text WHERE id = 1;

See the Update query syntax

Ravi Hirani February 2016

text is a reserved word in mysql.

Write your query as below:-

UPDATE sections SET `h1` = :h1, `text` = :text WHERE id = 1;

Hope it will help you :)

Post Status

Asked in February 2016
Viewed 3,988 times
Voted 6
Answered 2 times


Leave an answer