T
T
Turar Abu2015-12-27 09:09:23
PHP
Turar Abu, 2015-12-27 09:09:23

Why doesn't PDO work?

If you run the query with a regular PDO query, then it works:

$char = $page->for;

foreach( $_POST as $key => $value ){
  $value = str_replace("'", "\'", $value);
  $connect->query("UPDATE `keys` SET `value` = '$value' WHERE `name` = '$key' AND `for` = '$char'");
}

And if through prepare, then it does not work:
$char = $page->for;

    $query = $connect->prepare("UPDATE keys SET value = :v WHERE name = :n AND for = :c");
    $query->bindParam(':c', $char);

    foreach( $_POST as $key => $value ){
      $query->bindParam(':v', $value);
      $query->bindParam(':n', $key);
      $query->execute();

    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2015-12-27
@kemply

Because KEYS, VALUE, NAME and FOR are MySQL reserved words . If you want to use them as table or field names, use backticks.

M
Miku Hatsune, 2015-12-27
@Hatsune-Miku

You can just pass $_POST to execute()...

A
Andrey, 2015-12-27
@VladimirAndreev

Because sql queries are different. ` are very important

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question