php - Trouble with Prepared Statements in -
i working on website interface allow colleagues add tuples database. don't understand why code isn't working , hoping me please. thanks.
$query = "insert animal (mouseid,birthdate,harvestdate,injectiondate1, injectiondate2,injectiondate3,adjuvant,target,gender,age,animaltype, titer,antibodytype) values (?,?,?,?,?,?,?,?,?,?,?,?,?)"; $statement = $databaseconnection->prepare($query); $statement->bind_param('sssssssssisss', $_post['mouseid'], $_post['birthdate'], $_post['harvestdate'], $_post['injectiondate1'], $_post['injectiondate2'], $_post['injectiondate3'], $_post['adjuvant'], $_post['target'], $_post['gender'], $_post['age'], $_post['animaltype'], $_post['titer'], $_post['antibodytype']); $statement->execute(); $statement->close();
you aren't checking whether prepare
, execute
functions succeeded or not.
if aren't going check return function, can have pdo automatically check, , raise exception if database error occurs, setting attributes on connection:
$databaseconnection->setattribute(pdo::attr_errmode, pdo::errmode_exception);
if don't check return functions, code "swallowing" database errors happen.
setting these connection attributes isn't approach handling database errors, , isn't appropriate approach.
another alternative explicitly check return functions in code, example:
$sql = "insert ... "; $stmt = $dbh->prepare($sql); if(!$stmt) { print_r($dbh->errorinfo()); } if(!$stmt->execute()) { print_r($dbh->errorinfo()); }
using 1 of these approaches, it's revealed database error occurring. actual error code , message determine actual problem is. otherwise, we're guessing.
Comments
Post a Comment