سؤال

I'm trying to use a foreach loop to update database with PDO bound parameters. Everything seems to be set as desired, only problem is query doesn't execute and update the database as desired.

try {
    $dbh = new PDO('mysql:host=localhost;dbname=Batik', 'root', 'root');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $table = $_SESSION['table'];
    error_log($table);
    parse_str($_POST['pages'], $pageOrder);
    $query = "UPDATE $table SET `order` = ':key' WHERE `id` = ':value'";
    $STH = $dbh->prepare($query);


    foreach ($pageOrder['page'] as $key =>$value) {
        error_log($key.$value);
        $STH->bindParam(':value', $value);
        $STH->bindParam(':key', $key);
        //error_log(print_r($STH));
        //error_log(var_dump($STH));
        //error_log($STH->debugDumpParams());
        $STH->execute();

    }
} catch (PDOException $exception) {
    echo "There was an error updating your information, please contact James for assistance. "; 
    error_log($exception->getMessage());
  };

There are no more errors outputting to error.log. It simply won't update the database as desired. I have the query working fine with mysql_query, but when switching over to PDO I have lost functionality. Does anybody have any suggestions for further debugging tools?

هل كانت مفيدة؟

المحلول

You must not put placeholders in quotes in the SQL. It should be:

$query = "UPDATE $table SET `order` = :key WHERE `id` = :value";
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top