I do not understand why these lines do not work:
$host = 'localhost';
$dbname='mydbname';
$login='mylogin';
$pwd='mypwd';
$datex = date('Y/m/d H:i:s');
$nomx = 'jrmy';
$numx = '007';
try {
$bdd = new PDO('mysql:host='.$host.';dbname='.$dbname, $login, $pwd);
}
catch(Exception $e) {
die('Erreur : '.$e->getMessage());
}
$bdd->exec('INSERT INTO dossiers(date, nom, numero, disp) VALUES(\''.$datex.'\', \''.$nomx.'\', \''.$numx.'\', \'Y\')');
$id = $bdd->lastInsertId();
$bdd= null;
echo 'id: '.$id;
disclosure: the script worked on my apache server, and since I installed my script on a IIS server it stopped working...
AND the query function works perfectly... query OK, insert NO
EDIT : thank you all! really!
You need to tell PDO to raise an error
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Please note that the way you're handling exceptions is quite useless. It would be better to remove try-catch blocks from the code
You are not making full use of PDO. Use parameters to insert your data
Example:
$bdd->exec('INSERT INTO dossiers(date, nom, numero, disp) VALUES(?, ?,?, ?)');
$bdd->bindParam(1, $datex);
$bdd->bindParam(2, $nomx);
$bdd->bindParam(3, $numx);
$bdd->bindParam(4, 'Y');
And use this to raise errors:
$bdd->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With