Forum Moderators: coopster

Message Too Old, No Replies

MySQL PDO Result Always Returns True

         

nolim8ts

2:15 pm on Mar 1, 2010 (gmt 0)

10+ Year Member



Hi All,

I'm using the below code to update an item in my database. When I change the ID to one that does not exist in the database, the result always returns true.


try
{
$allowedTags='<p><strong><em><u><h1><h2><h3><h4><h5><h6><img>';
$allowedTags.='<li><ol><ul><span><div><br><ins><del>';

$news_id = 12;
$news_title = trim(stripslashes($_POST['news_title']));
$news_item = strip_tags(stripslashes($_POST['elm1']), $allowedTags);

$db_host = $config['db_host'];
$db_database = $config['db_database'];
$db_username = $config['db_username'];
$db_password = $config['db_password'];

$dbh = new PDO("mysql:host=$db_host;dbname=$db_database", "$db_username", "$db_password");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $dbh->prepare("UPDATE tbl_news SET title=?, body=? WHERE id=?");
$stmt->bindParam(1, $news_title);
$stmt->bindParam(2, $news_item);
$stmt->bindParam(3, $news_id);

$result = $stmt->execute();

if ($result)
{
echo 'record updated successfully';
}
else
{
echo 'record not updated';
}
}
catch(PDOException $e)
{
echo '<h4>'.$e->getMessage().'</h4>';
}
catch(Exception $e)
{
echo '<h4>'.$e->getMessage().'</h4>';
}

Matthew1980

2:29 pm on Mar 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there noLim8ts,

Sounds obvious, but have you echoed the sql query before its sent, and killed the script just after the echo to see exactly whats being sent to the database?

And where you can, put the obligatory or die(mysql_error()); on the end of the actual mysql_query() function to see if there is anything database side that isnt being caught by PHP?

Just thoughts, but errors like that usually have "wood for the trees" answers...

Cheers,
MRb