Page 1 of 1

Problem with PDO prepared insert

Posted: Sun Nov 21, 2010 7:51 pm
by nakins
Below is the code I've tried to use to insert data into a table. The code in the comments works. The other code does not. I don't know why. Help.

Code: Select all

/*
     $count = $DBH->exec("INSERT INTO assets(asset_name, date_added, short_desc) VALUES ('crappy', NOW(), 'some crappy crap')");
     echo $count;
     $DBH = null;
     */

     $stmt=$DBH->prepare("INSERT INTO assets(asset_name, date_added, short_desc) VALUES (:asset_name, NOW(), :short_desc)");

     $stmt->bindParam(':asset_name', $asset_name, PDO_PARAM_STR, 64);
     $stmt->bindParam(':short_desc', $short_desc, PDO_PARAM_STR, 64);

     $asset_name = 'more crap';
     $short_desc = 'some more crap';

     $stmt->execute();
     $stmt = null;

Re: Problem with PDO prepared insert

Posted: Sun Nov 21, 2010 9:08 pm
by McInfo
Enable errors.
error_reporting wrote:Notice: Use of undefined constant PDO_PARAM_STR - assumed 'PDO_PARAM_STR'
Warning: PDOStatement::bindParam() expects parameter 3 to be long, string given
The constant PDO_PARAM_STR does not exist. PDO::PARAM_STR does.

Re: Problem with PDO prepared insert

Posted: Sun Nov 21, 2010 10:24 pm
by nakins
Awesome!that fixed it. Now, if you could be so kind to tell me how you got that error message? I'm not getting any error messages back, except on connection (I tried setting the $user to something wrong to get a error message).

Thanks again. I've been working on this all weekend.

Code: Select all

 
try {
     $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
     $DBH->setAttribute( PDO_ATTR_ERRMODE, PDO::ERRMODE_WARNING );
     $stmt=$DBH->prepare("INSERT INTO assets(asset_name, date_added, short_desc) VALUES (:asset_name, NOW(), :short_desc)");

     $stmt->bindParam(':asset_name', $asset_name, PDO::PARAM_STR, 64);
     $stmt->bindParam(':short_desc', $short_desc, PDO::PARAM_STR, 64);

     $asset_name = 'more crap';
     $short_desc = 'some more crap';

     $stmt->execute();
     $stmt = null;
   }
   catch(PDOException $e) {
     echo "Syntax Error: ".$e->getMessage();
}

Re: Problem with PDO prepared insert

Posted: Sun Nov 21, 2010 10:32 pm
by McInfo
The errors (not to be confused with exceptions) are exposed by PHP when error_reporting and display_errors directives are enabled in the configuration settings (php.ini).