简体   繁体   English

PHP PDO查询问题

[英]PHP PDO query issue

I'm switching my project over to PDO and I'm having problems with the following code. 我将项目切换到PDO,并且以下代码出现问题。 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() . 我一直在阅读PDO,并且已经研究了DebugdumpParams() I added $result->debugDumpParams() and the page does not load. 我添加了$result->debugDumpParams() ,但页面未加载。

Do I need to use BindParam() after prepare() , before execute() to be able to debug? 我是否需要使用BindParam()之后prepare()execute()以能够调试?

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: 但是,如果您在PDO内打开错误报告,则可以从MYSQL中看到错误,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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