2

I'm switching my project over to PDO and I'm having problems with the following code. It does NOT throw an error, but also does NOT insert into the database. I've been reading up on PDO and I've looked into DebugdumpParams(). I added $result->debugDumpParams() and the page does not load.

Do I need to use BindParam() after prepare(), before execute() to be able to debug?

public function CustomerInsert($name, $street1, $street2, $city, $state, $zip, $phone_area, $phone, $email, $notes, $leadtype, $rating, $newsletter, $frequency)
    {
$q = "INSERT INTO customers VALUES('', :name , :street1 , :street2 , :city , :state , :zip , :phone_area , :phone , :email , :notes , :newsletter , :leadtype , :frequency )";

try{
$result = $this->connection->prepare($q); 

$result->execute(array(':name'=>$name, ':street1'=>$street1, ':street2'=>$street2, 
                                ':city'=>$city, ':state'=>$state, ':zip'=>$zip, ':phone_area'=>$phone_area, 
                                ':phone'=>$phone, ':email'=>$email, ':notes'=>$notes, ':newsletter'=>$newsletter, 
                                ':leadtype'=>$leadtype, ':frequency'=>$frequency));
}
catch (PDOException $e) 
{  
   throw new Exception('Connection failed: ' . $e->getMessage());
 }
8
  • Turn error reporting on Commented Apr 10, 2013 at 15:09
  • How do I do that? Error reporting in PHP? Is it possible to do that on a per-file basis? Commented Apr 10, 2013 at 15:10
  • And also look at what's returned by $result->ErrorInfo() and $this->connection->ErrorInfo() Commented Apr 10, 2013 at 15:11
  • yeah, you probably have a warning trailing somewhere. Silly question of mine I hope you won't mind, but are you sure it is possible to specify an insert without adding the fields before the VALUES(...) part? Commented Apr 10, 2013 at 15:14
  • (As a wild stab in the dark, I'd suggest removing the intial '' in your INSERT - is that where the auto_number ID field is?) Commented Apr 10, 2013 at 15:15

1 Answer 1

1

Your query doesn't look malformed, so my guess would be that the number of values does not match the number of fields in the table. You can fix this by specifying the fields in your query:

$q = "INSERT INTO `customers` 
      (`name`,`street1`,`street2`,`city`,`state`,`zip`,`phone_area`,`phone`,`email`,`notes`,`newsletter`,`leadtype`,`frequency`)
      VALUES(:name , :street1 , :street2 , :city , :state , :zip , :phone_area , :phone , :email , :notes , :newsletter , :leadtype , :frequency )";

But you'd be able to see the error from MYSQL if you turn error reporting on within PDO, e.g.:

$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh, sweet, that is throwing an error now with a column. I think I can figure it out now. Will update in a minute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.