Page 1 of 1

PHP PDO problem..

Posted: Tue Jan 23, 2007 9:14 am
by seanferd
HI,

I'm having some problems getting PDO to cooperate with me. I can connect to my DB, then do a PDO->query() inside a foreach just fine. The problem occurrs when I try to do anything but a select statement. Here's a snippet.

Code: Select all

$db = new PDO('mysql:host=localhost;dbname=pictures', user, 'password' );
$db->beginTransaction();
$db->exec( "INSERT INTO pics (description) VALUES (" . $desc . ") WHERE id =" . $picid );
$db->commit();
echo( "Your description has been added successfully." ) ;
The code never throws any errors, and acts like everything went successfully. However, there are no additions to my DB. I'm completely baffled. Mostly because $db->query() inside a foreach works like a charm. Any ideas??

I'm using PHP 5.1 and mySQL 5.0 with PDO on Debian Etch. Running 2.6.17-2-686 kernel.

Thank you in advance for your responses!

EDIT: I just checked my mysql.tables_priv table. My user has Select and Update permissions on my pictures DB. Thanks --Sean

Posted: Tue Jan 23, 2007 9:29 am
by volka
try

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);

$db = new PDO('mysql:host=localhost;dbname=pictures', user, 'password' );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db->beginTransaction();
$db->exec( "INSERT INTO pics (description) VALUES (" . $desc . ") WHERE id =" . $picid );
$db->commit();
echo( "Your description has been added successfully." );
There's no WHERE clause on an INSERT statement.

Posted: Tue Jan 23, 2007 9:39 am
by seanferd
Dang it. My bad for putting that up there. This was what I had originally and it didn't work either.

Code: Select all

$db->exec( "UPDATE pics set description=" . $desc . " WHERE id =" . $picid );
So out of desperation I was trying an INSERT, which obviously didn't work either.

Posted: Tue Jan 23, 2007 9:42 am
by volka
anyway, have you tried the error_reporting and setAttribute part?

Posted: Tue Jan 23, 2007 9:49 am
by seanferd
Awesome! It was throwing syntax errors because of spaces in description. I forgot to put quotes around it in the UPDATE. Rookie mistake. :)

Code: Select all

$db->exec( "UPDATE pics set description='" . $desc . "' WHERE id =" . $picid );
Thank you for your help, volka!

Posted: Tue Jan 23, 2007 10:02 am
by volka
spaces or no spaces, there have to be quotes around a string literal for mysql