gpt4 book ai didi

php - PDO 插入不会添加到数据库

转载 作者:行者123 更新时间:2023-11-29 06:38:51 25 4
gpt4 key购买 nike

我最近一直在使用 PDO,所以我是新手,出于某种原因我无法使用它来将记录插入数据库。我在这里做错了什么!?!?

if (isset($_POST['submit'])) {
$sql = "INSERT INTO customer(firstname,lastname,email,address,city,state,zip,phone,company)
VALUES(:firstname,:lastname,:email,:address,:city,:state,:zip,:phone,:company)";

$stmt = $db->prepare($sql);

if (!$_POST['fname'])
$errors[] = 'You must input a first name';
if (!$_POST['lname'])
$errors[] = 'You must input a last name';
if (!$_POST['address'])
$errors[] = 'You must input an address';
if (!$_POST['city'])
$errors[] = 'You must input a city';
if (!$_POST['state'])
$errors[] = 'You must input a state';
if (!$_POST['zip'])
$errors[] = 'You must input a zip';
if (!$_POST['email'])
$errors[] = 'You must input a e-mail';
if (!$_POST['phone'])
$errors[] = 'You must input a phone';

if (!$errors) {
$stmt->bindParam(':firstname', $_POST['fname'], PDO::PARAM_STR);
$stmt->bindParam(':lastname', $_POST['lname'], PDO::PARAM_STR);
$stmt->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
$stmt->bindParam(':address', $_POST['address'], PDO::PARAM_STR);
$stmt->bindParam(':city', $_POST['city'], PDO::PARAM_STR);
$stmt->bindParam(':state', $_POST['state'], PDO::PARAM_STR);
$stmt->bindParam(':zip', $_POST['zip'], PDO::PARAM_STR);
$stmt->bindParam(':phone', $_POST['phone'], PDO::PARAM_STR);
$stmt->bindParam(':company', $_POST['company'], PDO::PARAM_STR);

$stmt->execute();
}
}

当我执行 print_r($db->e​​rrorInfo());提交后我得到这个数组返回给我

Array ( [0] => 00000 [1] => [2] => ) 1

最佳答案

来自 http://www.php.net/manual/en/pdo.errorinfo.php :

PDO::errorInfo() only retrieves error information for operations performed directly on the database handle. If you create a PDOStatement object through PDO::prepare() or PDO::query() and invoke an error on the statement handle, PDO::errorInfo() will not reflect the error from the statement handle. You must call PDOStatement::errorInfo() to return the error information for an operation performed on a particular statement

因此,要查找实际错误,请使用 $stmt->errorInfo()。如果该信息不能进一步帮助您,请将您遇到的错误添加到问题中或提出新问题。

关于php - PDO 插入不会添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22814178/

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