0

I'm trying to come up with a way to control what happends if some UPDATE/INSERTS into a BD fail.

private $db;
public function __construct() {
    $dsn = 'mysql:dbname=BDNAME;host=localhost';
    $this->db = new PDO($dsn, 'USEER', 'PASSWORD', array('charset' => 'utf8'));
}


public function createnewthingtodo($what,$where,$when,$how,$done){

 $sql= "INSERT INTO TODOLIST (what,where,when,how,done) VALUES (:what, :where, :when, :how, :done)";
    $st = $this->db->prepare ($sql);
    $st->bindValue(':what',$what);
    $st->bindValue(':where',$where);
    $st->bindValue(':when',$when);
    $st->bindValue(':how',$how);
    $st->bindValue(':done',$done);
     if($st->execute()){
        return true;
     }else{
        return false;
     }
    }

This example is returning always false, if i delete the last "if" and put the execution part just below the last binvalue, it works just fine, but then i end up with no error handling, any ideas on how can i achieve this? Thanks for your time.

Shadow
  • 33,525
  • 10
  • 51
  • 64
Javier
  • 25
  • 4
  • 1
    Maybe use backticks? `where` looks a lot like a mysql keyword – brombeer Jan 11 '23 at 11:27
  • It does not depend of error handling....? [https://www.php.net/manual/en/pdo.error-handling.php]https://www.php.net/manual/en/pdo.error-handling.php – Juan Jan 11 '23 at 11:28
  • Srry, brombeer, editing the question i deleted it, in the real code the } is in it's place. I will edit the question. Thanks. – Javier Jan 11 '23 at 11:31
  • `i end up with no error handling`...you already have no error handling - you're completely ignoring any errors, which, I would think, is the main reason why you have no idea what's wrong, and therefore no idea how to fix the query. Please read this: https://phpdelusions.net/pdo#errors – ADyson Jan 11 '23 at 11:35
  • Also have a look at [Why does this PDO statement silently fail?](https://stackoverflow.com/questions/32648371/why-does-this-pdo-statement-silently-fail) – ADyson Jan 11 '23 at 11:39

0 Answers0