gpt4 book ai didi

php - SQLSTATE[23000] : Integrity constraint violation: 1048 Column 'ID' cannot be null

转载 作者:行者123 更新时间:2023-11-29 16:00:47 31 4
gpt4 key购买 nike

我正在尝试创建用户注册表单。但是,当在本地计算机上测试它时,出现错误:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ID' cannot be null.

我在 AMPPS Apache、PHP 7.1/MySQL 上运行它。我的操作系统是 macOS Mojave 10.4。我基本上已经尝试了之前答案中“完整性约束违规:1048”中建议的所有内容,但仍然没有任何线索。我希望有人能提供帮助。感谢您检查!

这是 php:

$event_owner_id=$_POST['event-owner-id'];
$event_owner_familyname=$_POST['event-owner-familyname'];
$event_owner_firstname=$_POST['event-owner-firstname'];
$event_owner_familyname_yomi=$_POST['event-owner-familyname-yomi'];
$event_owner_firstname_yomi=$_POST['event-owner-firstname-yomi'];
$event_owner_creditname=$_POST['event-owner-creditname'];
$event_owner_post=$_POST['event-owner-post'];
$event_owner_address=$_POST['event-owner-address'];
$event_owner_phone=$_POST['event-owner-phone'];
$event_owner_mail=$_POST['event-owner-mail'];
$event_owner_pass=$_POST['event-owner-pass'];
$event_owner_pass_kaku=$_POST['event-owner-pass-kaku'];

try {
$dsn = 'mysql:host=localhost;dbname=xxx;charset=utf8';
$pdo = new PDO($dsn, $db['user'], $db['pass'], array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
$sql = 'INSERT INTO User (ID, Type, LastName, FirstName, LastNameKana, FirstNameKana, Credit, PostCode, Address, PhoneNumber, eMailAddress, Password) VALUES (:ID, 002, :LastName, :FirstName, :LastNameKana, :FirstNameKana, :Credit, :PostCode, :Address, :PhoneNumber, :eMailAddress, :Password)';
$stmt=$pdo->prepare($sql);
$stmt -> bindParam(":ID", $event_owner_id, PDO::PARAM_STR);
$stmt -> bindParam(":LastName", $event_owner_familyname, PDO::PARAM_STR);
$stmt -> bindParam(":FirstName", $event_owner_firstname, PDO::PARAM_STR);
$stmt -> bindParam(":LastNameKana", $event_owner_familyname_yomi, PDO::PARAM_STR);
$stmt -> bindParam(":FirstNameKana", $event_owner_firstname_yomi, PDO::PARAM_STR);
$stmt -> bindParam(":Credit", $event_owner_creditname, PDO::PARAM_STR);
$stmt -> bindParam(":PostCode", $event_owner_post, PDO::PARAM_INT);
$stmt -> bindParam(":Address", $event_owner_address, PDO::PARAM_STR);
$stmt -> bindParam(":PhoneNumber", $event_owner_phone, PDO::PARAM_INT);
$stmt -> bindParam(":eMailAddress", $event_owner_mail, PDO::PARAM_STR);
$stmt -> bindParam(":Password", $event_owner_pass, PDO::PARAM_STR);
$stmt->execute();
}
catch (PDOException $e) {
echo $e->getMessage();
}

$pdo=null;

最佳答案

正如 aynber 所建议的,ID 变量实际上为 null。
我的注册表应该包含 3 页:
1. HTML表单
2.确认页面
3.数据库插入和成功消息页面。
这些变量只传递到确认页面,当实际插入数据库时​​,它们确实都是空的。我能够通过 session 解决这个问题:

//form page HTML:
<form method="post" action="confirmation.php">
<input type="text" name="user-id" >

//confirmation page PHP:
session_start();
$_SESSION['user-id']=$_POST['user-id'];

//database insert and success message page PHP:
session_start();
$user_id = $_SESSION['user-id'];

感谢大家的帮助。
感谢 Jay Blanchard 关于密码安全的说明。我一定会解决这个问题。

关于php - SQLSTATE[23000] : Integrity constraint violation: 1048 Column 'ID' cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56222068/

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