繁体   English   中英

PDO错误-列不能为空

[英]PDO Error - column cannot be null

我正在尝试运行此PHP PDO查询:

$stmt = $pdo_conn->prepare("INSERT into reseller (company, title, forename, surname, address, phone, email, account, bill_by_email, bill_by_post, bill_by_sms, sms_phone, callplan, linked_customer, upload_invoices, show_itemised_calls, support_tickets, support_tickets_emails) values (:company, :title, :forename, :surname, :address, :phone, :email, :account, :bill_by_email, :bill_by_post, :bill_by_sms, :sms_phone, :callplan, :linked_customer, :upload_invoices, :show_itemised_calls, :support_tickets, :support_tickets_emails) ");
    $stmt->execute(array(':company' => ($_POST["company"]), 
    ':title' => ($_POST["title"]), 
    ':forename' => ($_POST["forename"]), 
    ':surname' => ($_POST["surname"]), 
    ':address' => ($_POST["address"]), 
    ':phone' => ($_POST["phone"]), 
    ':email' => ($_POST["email"]), 
    ':account' => ($_POST["account"]), 
    ':bill_by_email' => ($_POST["bill_by_email"]), 
    ':bill_by_post' => ($_POST["bill_by_post"]), 
    ':bill_by_sms' => ($_POST["bill_by_sms"]), 
    ':sms_phone' => ($_POST["sms_phone"]), 
    ':callplan' => ($_POST["callplan"]), 
    ':linked_customer' => ($_POST["linked_customer"]), 
    ':upload_invoices' => ($_POST["upload_invoices"]), 
    ':show_itemised_calls' => ($_POST["show_itemised_calls"]), 
    ':support_tickets' => ($_POST["support_tickets"]), 
    ':support_tickets_emails' => ($_POST["support_tickets_emails"]) ));

但是当我有空白字段时,我会收到此错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'bill_by_post' cannot be null' in /home/integra/public_html/lifeline/reseller/addreseller.php:34 Stack trace: #0 /home/integra/public_html/lifeline/reseller/addreseller.php(34): PDOStatement->execute(Array) #1 {main} thrown in /home/integra/public_html/lifeline/reseller/addreseller.php on line 34

阻止这种情况发生的最佳方法是什么? 改用bindParam()更好吗?

谢谢

你可以:

  • 更改您的字段以允许NULL
  • 在尝试使用它之前检查设置的值
  • 更改数据库字段以具有某种默认值; 即使该值是一个空字符串,例如bill_by_post NOT NULL DEFAULT '' (或等效值,具体取决于您使用的数据类型)

好像该列需要数据库中的值,并且您发送的var为null / empty。

只要$ _POST [“ value”]为空,就在dbase上更改它或检索错误,因此用户知道他必须填补空白,甚至不发送它,否则您将收到错误消息。

不,回答您的问题,如果在数据库中设置了一些要求,那么您插入的数据必须符合这些要求,否则将返回致命错误。

您可以严格分配每个列变量:

$stmt->execute(array(
    ':title' => (string)($_POST["title"]), 
    ':number' => (int)($_POST["number"])
    ));

PHP类型杂耍

暂无
暂无

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

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