Marc
Marc

Reputation: 9537

PDO SQL - Update query issue

I am new to pdo and do not get why the following insert query does not work. If I remove the line that executes the query, there will be of course no insertion, but there will be no error. If I leave that line, the script is not executed. Of course I checked and rechecked the table name and field name. Hope someone can hep me understand. Note that before executing the query, the ber_mBacth_date field of my table is set to NULL. Cheers. Marc

<?php
$db_host = 'localhost';  
$db_user = 'user';  
$db_password = 'user';  
$db_database = 'myconsole';               

$mBatchDate = date('Y-m-d H:i:s');

$connexion = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password);
$qry = $connexion->execute('UPDATE batcherrors SET ber_mBatch_date = "'.$mBatchDate.'"');

$connexion = NULL;
?>

Upvotes: 0

Views: 121

Answers (1)

Michał Powaga
Michał Powaga

Reputation: 23183

Can you try instead of:

$connexion = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password);
$qry = $connexion->execute('UPDATE batcherrors SET ber_mBatch_date = "'.$mBatchDate.'"');

do:

$statement = $connexion->prepare("UPDATE batcherrors SET ber_mBatch_date = :mBatchDate");
$statement->bindValue(':mBatchDate', $mBatchDate, PDO::PARAM_STR);
$statement->execute();

Binding is recommended way to set parameters values (over concatenation).

Upvotes: 1

Related Questions