简体   繁体   中英

PHP PDO query issue

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());
 }

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 , eg:

$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM