PHP Update Data in MySQL using PDO -
this question has answer here:
i'm trying update data in mysql using pdo.
i have set code below error
sqlstate[42000]: syntax error or access violation: 1064 have error in >your sql syntax; check manual corresponds mysql server version >for right syntax use near '(h1, text) values ('blah blah' @ line 1
if (isset($_post['submit']))://if admin wants 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 pdo error mode exception $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); // prepare statement $stmt = $conn->prepare('update sections (h1, text) values (:h1, :text) id=1'); $stmt->bindparam(':h1', $h1); $stmt->bindparam(':text', $text); // execute query $stmt->execute(); // echo message update succeeded echo $stmt->rowcount() . " records updated successfully"; } catch(pdoexception $e) { echo $sql . "<br>" . $e->getmessage(); } $conn = null;
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 id = 1;
Comments
Post a Comment