SQLSTATE [23000]:完整性约束违规:1048列'first_name'不能为空

时间:2017-01-15 14:16:24

标签: php mysql pdo

我有一个应该插入数据的注册表单,但我总是在提交表单后出现此错误

  

SQLSTATE [23000]:完整性约束违规:1048列   'first_name'不能为null

HTML下的sign-up.php

<div class="form-group">
                <label>Complete Name:</label>
                <input type="text" class="form-control" name="fname" placeholder="First Name"><br>
                <input type="text" class="form-control" name="mname" placeholder="Middle Name"><br>
                <input type="text" class="form-control" name="lname" placeholder="Last Name (this will be your username)" 
                value="<?php if(isset($error)){echo $uname;}?>"><br>
            </div>
            <div class="form-group">
                <input type="password" class="form-control" name="txt_upass" placeholder="Password (more than 6 characters)"><br>
            </div>

PHP下的sign-up.php

if(isset($_POST['btn-signup']))
{   
    $fName = strip_tags($_POST['fname']);
    $mName = strip_tags($_POST['mname']);
    $uname = strip_tags($_POST['lname']);
    $upass = strip_tags($_POST['txt_upass']);

    if($uname=="")  {
        $error[] = "provide username !";    
    }
    else if($upass=="") {
        $error[] = "provide password !";
    }
    else if(strlen($upass) < 6){
        $error[] = "Password must be atleast 6 characters"; 
    }
    else
    {
        try
        {
            $stmt = $user->runQuery("SELECT last_name FROM users WHERE last_name=:uname");
            $stmt->execute(array(':uname'=>$uname));
            $row=$stmt->fetch(PDO::FETCH_ASSOC);

            if($row['last_name']==$uname) {
                $error[] = "Username already taken !";
            }
            else
            {
                if($user->register($fName,$mName,$uname,$upass)){   
                    $user->redirect('sign-up.php?joined');
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }   
}

class.user.php下的PHP函数

public function register($fName,$mName,$uname,$upass)
    {
        try
        {
            $new_password = password_hash($upass, PASSWORD_DEFAULT);

            $stmt = $this->conn->prepare("INSERT INTO users(first_name,middle_name,last_name,user_pass) 
                                                       VALUES(:fName,:mName,:uname,:upass)");

            $stmt->bindparam(":fName", $first_name);
            $stmt->bindparam(":mName", $middle_name);                                    
            $stmt->bindparam(":uname", $last_name);
            $stmt->bindparam(":upass", $new_password);                                            

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }

3 个答案:

答案 0 :(得分:2)

变量名称不同,请在此处更改

        $stmt->bindparam(":fName", $fName);
        $stmt->bindparam(":mName", $mName);                                    
        $stmt->bindparam(":uname", $uname);
        $stmt->bindparam(":upass", $upass);                                            

答案 1 :(得分:1)

确保您绑定了所有您在$stmt->bindparam(":fName", $first_name)函数中使用的变量名称。

而不是: $stmt->bindparam(":fName", $fname)
它应该是:first_name

...错误意味着fragment在数据库中不能为空。

答案 2 :(得分:0)

我知道了,我在bindparam中使用了错误的参数,第二个参数应该是$ fName而不是$ first_name。