gpt4 book ai didi

php - PDO 错误 - 列不能为空

转载 作者:行者123 更新时间:2023-11-29 13:02:35 26 4
gpt4 key购买 nike

我正在尝试运行此 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 ''(或同等内容,具体取决于您正在使用的数据类型)

关于php - PDO 错误 - 列不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23133989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com