2

I'm trying to update a single element in one of my tables using a PDO prepared statement, and for some reason it's not working. I'm using try-and-catch and I'm receiving no errors from the system. I've also echoed both of my bound parameters, and they are both registering in the system, so I don't know why it's not going through. I've used a query very similar to this in another script and everything was fine.

if($_POST['check_request'] == "Yes"){
    $check_amnt = $_POST['check_amnt'];
    try {
        $STH = $DBH->prepare('UPDATE accounts SET check = :check_amnt WHERE accnt = :user');
        $STH->bindParam(':check_amnt', $check_amnt);
        $STH->bindParam(':user', $ulog);
        $STH->execute();
    }
    catch(PDOException $e) {  
        echo "Check Input Error: " .$e->getMessage(). "</br>";
    }
}
user1562781
  • 379
  • 1
  • 9
  • 20
  • 1
    The PDO part of this code looks ok. Check if the variables are set properly, p.e. `$_POST['check_request']`, `$check_amnt` and `$ulog`. – Bjoern Aug 30 '12 at 05:36
  • Every variable has been checked and they're all registering in the system. – user1562781 Aug 30 '12 at 05:38
  • Then do a `die('Hello!');` or something similar after `$STH->execute()` to see the code really gets there. – Bjoern Aug 30 '12 at 05:45
  • Okay, so I just got an error to produce based on xdazz suggestions. This is the error I'm getting but I'm not sure why it's wrong: "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 'check = '40' WHERE accnt = 'admin'' at line 1" – user1562781 Aug 30 '12 at 05:47
  • `echo` the SQL query itself (the one in your prepare-statement) and try to execute this manually with another tool (phpmyadmin, sqlyog, mysql commandline or something like this). – Bjoern Aug 30 '12 at 05:48

1 Answers1

3

Did you set the exception mode for PDO with:

$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Update: check is mysql reserved words, you need to escape it.

$STH = $DBH->prepare('UPDATE accounts SET `check` = :check_amnt WHERE accnt = :user');
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • This is perfect. I'm now receiving errors. This is the error I'm getting but I'm not sure why the syntax is wrong. Any suggestions? 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 'check = '40' WHERE accnt = 'admin'' at line 1 – user1562781 Aug 30 '12 at 05:46
  • You're a genius. I didn't even catch that one. Thanks! – user1562781 Aug 30 '12 at 06:02