PDO insert not working

the following should work but i am unable to spot why


$sth2 = $db->prepare ("INSERT INTO userpie_history (`to`,`user`,`message`,`ip`,`useragent`) VALUES (:to,:user,:message,:ip,:useragent)");
$sth2->bindParam(':to', $to, PDO::PARAM_STR);
$sth2->bindParam(':user', $user, PDO::PARAM_STR);
$sth2->bindParam(':message', $message, PDO::PARAM_STR);
$sth2->bindParam(':ip', $ip, PDO::PARAM_STR);
$sth2->bindParam(':useragent', $useragent, PDO::PARAM_STR);
$sth2->execute();

the page does not throw any errors but i think that’s ngnix default but i have basically copied and altered this statement from another script i’ve made in the past and it works but i can not see why now even tho i have set all the values to what they need to be.

If you are seeing a blank page, that typically means you haven’t turned on error reporting.

http://php.net/manual/en/function.error-reporting.php

Additionally, you should surround your PDO code with a try/catch block, so you can catch an error when/if it fails so that it doesn’t kill the whole page or display an unwanted error message in the wrong place (or show an error, if you’re looking for one).

try {
 //your PDO code
} catch (PDOException $e) {
  echo "Database error: ".$e->getMessage();
}

http://us3.php.net/PDOException

Hi, Try this…:slight_smile:


 try
	 {
	
	
	  $db = new PDO('mysql:host=localhost;dbname=MYDB','root','');
          $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); //This will help to show errors when using PDO.

	  $sth2 = $db->prepare ("INSERT INTO userpie_history (`to`,`user`,`message`,`ip`,`useragent`) VALUES (:to,:user,:message,:ip,:useragent)");
          $sth2->bindParam(':to', $to, PDO::PARAM_STR);
         $sth2->bindParam(':user', $user, PDO::PARAM_STR);
         $sth2->bindParam(':message', $message, PDO::PARAM_STR);
         $sth2->bindParam(':ip', $ip, PDO::PARAM_STR);
         $sth2->bindParam(':useragent', $useragent, PDO::PARAM_STR);
         $sth2->execute();
	 $db = null;
	 }
	
	 catch(PDOException $e)
	 {
	   echo $e->getMessage();
	 }