列“ custID”不能为空

时间:2019-05-04 01:56:15

标签: php mysql

我正在尝试使用此php文档来使用一种形式将信息输入数据库。我不断收到相同的错误,列“ custID”不能为null。我不知道怎么了或怎么做。我可能需要为此任务取L,但是如果以后能遇到同样的问题,我可以得到一个答案,那将很有帮助。

我已经尝试在mysql代码中执行NOT NULL AUTO_INCREMENT。我还尝试通过对ustID使用NULL来做同样的事情。两者都不起作用。

if(isset($_POST['submit'])){
        $data_missing = array();            

        if(empty($_POST['custID'])){
            $data_missing[] = 'Customer ID';
            }else{
                $custID = trim($_POST['custID']);
            }
        if(empty($_POST['custFirstName'])){
            $data_missing[] = 'First Name';
            }else{
                $custFirstName = trim($_POST['custFirstName']);
            }
        if(empty($_POST['custLastName'])){
            $data_missing[] = 'Last Name';
            }else{
                $custLastName = trim($_POST['custLastName']);
            }
        if(empty($_POST['address'])){
            $data_missing[] = 'Address';
            }else{
                $address = trim($_POST['address']);
            }
        if(empty($_POST['city'])){
            $data_missing[] = 'city';
            }else{
                $city = trim($_POST['city']);
            }
        if(empty($_POST['custstate'])){
            $data_missing[] = 'State';
            }else{
                $custstate = trim($_POST['custstate']);
            }
        if(empty($_POST['custEmail'])){
            $data_missing[] = 'Email';
            }else{
                $custEmail = trim($_POST['custEmail']);
            }
        if(empty($_POST['custPhone'])){
            $data_missing[] = 'Phone';
            }else{
                $custPhone = trim($_POST['custPhone']);
            }
        if(empty($_POST['Password'])){
            $data_missing[] = 'Password';
            }else{
                $Password = trim($_POST['Password']);
            }
    }

    if(empty($data_missing)){
        require_once '../LabYourLastProject/mysqli_connect.php';
        $query =  "INSERT INTO Customers (custID, custFirstName, custLastName, address, city,"
                . " custstate, custEmail, custPhone, Password) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        $stmt = mysqli_prepare($dbc, $query);

        mysqli_stmt_bind_param($stmt, "sssssssss", $custID, $custFirstName,$custLastName, $address, $city, $custstate, $custEmail, $custPhone, $Password);

        mysqli_stmt_execute($stmt);

        $affected_rows = mysqli_stmt_affected_rows($stmt);

        if($affected_rows == 1){
            echo 'Student Entered';
            mysqli_stmt_close($stmt);
            mysqli_close($dbc);
        }else{
            echo 'Error Occurred <br />';
            echo mysqli_error($dbc);
        }
    }else{
            echo'You need to enter the following data<br />';
            foreach($data_missing as $missing){
                echo "$missing<br />";
            }
        }

它应该将从表单传递的数据插入另一个文件中,并显示缺少的数据。我刚得到错误。

2 个答案:

答案 0 :(得分:0)

您必须删除custID,因为它是AUTO_INCREMENT

 $query =  "INSERT INTO Customers (custFirstName, custLastName, address, city,"
                . " custstate, custEmail, custPhone, Password) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

和此代码

    mysqli_stmt_bind_param($stmt, "sssssssss", $custFirstName,$custLastName, $address, $city, $custstate, $custEmail, $custPhone, $Password);

答案 1 :(得分:0)

在向数据库表中插入数据时,需要省略主键custID,因为此处没有必要。服务器会注意这一点。不错,您已经将其设为auto_increment。仅当您具有唯一的值时,才可以将其包括在插入代码中。但是在正常情况下,请将其留空并插入其他字段

$query =  "INSERT INTO Customers (custFirstName, custLastName, address, city,"
                . " custstate, custEmail, custPhone, Password) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";