doubianyu7844 2019-05-20 13:34 采纳率: 0%
浏览 1837

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

我正在尝试创建一个用户注册表格。但是,在本地机器上测试它时,我会得到一个错误:

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

我正在AMPPS Apache PHP7.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条回答 默认 最新

  • duanmou9228 2019-05-24 14:39
    关注

    As aynber suggested, the ID variable was actually null.
    My registration form was supposed to include 3 pages:
    1. HTML form
    2. confirmation page
    3. database insert and success message page.
    The variables were passed only to confirmation page, and when it came to actual database insert, they indeed were all null. I was able to solve this with sessions:

    //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'];
    

    Thanks everyone for your help.
    Thanks Jay Blanchard for your note on password security. I will definitely work on that issue.

    评论

报告相同问题?