-2

I can update text strings (varchar) types to my Mysql database with PDO without any problems. but with integer (int(11)) types - my PDOstatement has some big problems and also can not write the integer value to the database.

here you can see the error message I get by putting integer values for UPDATE:

ERRNO:42000 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 'age = '800' WHERE id = '1'' at line 1

I am getting the values from $_POST like this:

foreach ($_POST['changed'] as $SubArray) {
    foreach ($SubArray as $key => $value) {
        if ($key === 'recid') continue;


        $sql  = "UPDATE clients SET $key = :value WHERE id = :recid";

        $STH = $DBH->prepare($sql);

        try {

            $STH->execute(array(':value' => $value, ':recid' => $SubArray['recid']));

        } catch (PDOException $e) {
            $code = $e->getCode();
            $file = $e->getFile();
            $line = $e->getLine();
            $msg  = $e->getMessage();
            echo "$file:$line ERRNO:$code ERROR:$msg";
        }
    }
}

echo urlencode($sql) => UPDATE+clients+SET+age+%3D+%3Avalue+WHERE+id+%3D+%3Arecid

18
  • 1
    Have you made sure that the type for the column in the database is set to Integer?
    – iswinky
    Sep 3, 2013 at 7:40
  • 1
    Have you done what the error message suggested to you? Have you consulted the Mysql manual of your server version? What did you find out?
    – hakre
    Sep 3, 2013 at 7:42
  • 2
    A syntax error has nothing to do with binding. It's a problem with the query. What about echoing your query out for the visual inspection? Sep 3, 2013 at 7:44
  • 1
    wait. what is that? I mean real $sql variable. and better urlencode()-d to reveal all possible non-printable characters Sep 3, 2013 at 8:19
  • 1
    It would seem one of your column names is a reserved word. Consult the MySQL Manual to see what words are reserved. For maximum compatibility, you should always wrap MySQL table and column names in backticks (other engines have their own mechanisms for this). Note: since you haven't updated your PHP code sample, we cannot see how/where you are logging $sql, so there is no real guarantee that you logged the query that actually crashed.
    – DCoder
    Sep 3, 2013 at 8:42

1 Answer 1

0

setting up the query ($sql) like this, does the trick.

$sql = "UPDATE `clients` SET `$key` = :value WHERE id = :recid";

before and after table and column name use backticks

`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.