SQLSTATE [23000]:违反完整性约束:1048列“ ID”不能为空

时间:2019-05-20 13:34:09

标签: php mysql sql insert bindparam

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

  

SQLSTATE [23000]:违反完整性约束:1048列“ ID”   不能为空。

我正在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;

1 个答案:

答案 0 :(得分:0)

正如aynber所建议的,ID变量实际上为空。
我的注册表应该包括3页:
1. HTML表单
2.确认页面
3.数据库插入和成功消息页面。
这些变量仅传递到确认页面,并且在实际插入数据库时​​,它们确实全部为空。我可以通过以下会话解决此问题:

//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关于密码安全性的说明。我一定会解决这个问题。

相关问题