PHP PDO problem..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seanferd
Forum Newbie
Posts: 3
Joined: Mon Jan 22, 2007 5:14 pm

PHP PDO problem..

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
seanferd
Forum Newbie
Posts: 3
Joined: Mon Jan 22, 2007 5:14 pm

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

anyway, have you tried the error_reporting and setAttribute part?
seanferd
Forum Newbie
Posts: 3
Joined: Mon Jan 22, 2007 5:14 pm

Post 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!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

spaces or no spaces, there have to be quotes around a string literal for mysql
Post Reply