SQLSTATE [23000]:违反完整性约束:1048列'性别'不能是nullsorry,Query无法执行

时间:2017-01-02 07:48:11

标签: php mysql pdo

它是一个PDO注册脚本。没有性别,dob,ip的工作正常。我添加了三个新列dob,Gender,ip。添加这些列后,它会显示这些错误。我无法弄清楚问题是什么。

signup.php

if(isset($_POST['btn-signup']))
{
    $uname = trim($_POST['txtuname']);
    $email = trim($_POST['txtemail']);
    $upass = trim($_POST['txtpass']);
    $dob = trim($_POST['dob']);
    $Gender = trim($_POST['sex']);
    $ip = $_SERVER['REMOTE_ADDR'];
    $code = md5(uniqid(rand()));

    $stmt = $reg_user->runQuery("SELECT * FROM tbl_users WHERE userEmail=:email_id");
    $stmt->execute(array(":email_id"=>$email));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if($stmt->rowCount() > 0)
    {
        $msg = "
              <div class='alert alert-error'>
                <button class='close' data-dismiss='alert'>&times;</button>
                    <strong>Sorry !</strong>  email allready exists , Please Try another one
              </div>
              ";
    }

<?php if(isset($msg)) echo $msg;  ?>
      <form class="form-signin" method="post">
        <h2 class="form-signin-heading">Sign Up</h2><hr />
        <input type="text" class="input-block-level" placeholder="Username" name="txtuname" required />
        <input type="email" class="input-block-level" placeholder="Email address" name="txtemail" required />
        <input type="password" class="input-block-level" placeholder="Password" name="txtpass" required />
        <input type="date" class="input-block-level" placeholder="dob" name="dob" required />
        <input type="text" class="input-block-level" placeholder="Gender" name="sex" />
        <hr />
        <button class="btn btn-large btn-primary" type="submit" name="btn-signup">Sign Up</button>
        <a href="index.php" style="float:right;" class="btn btn-large">Sign In</a>
      </form>

class.user.php

public function register($uname,$email,$upass,$code) {
    try {                           
        $password = md5($upass);
        $stmt = $this->conn->prepare("INSERT INTO tbl_users(userName, userEmail, 
        userPass, tokenCode, Gender, dob,ip) VALUES(:user_name, :user_mail, :user_pass, :active_code, :Gender_l, :dob_l, :ip_l)");
        $stmt->bindparam(":user_name",$uname);
        $stmt->bindparam(":user_mail",$email);
        $stmt->bindparam(":user_pass",$password);
        $stmt->bindparam(":active_code",$code);
        $stmt->bindparam(":Gender_l",$Gender);
        $stmt->bindparam(":dob_l",$dob);
        $stmt->bindparam(":ip_l",$ip);
        $stmt->execute();   
        return $stmt;
    } catch(PDOException $ex) {
        echo $ex->getMessage();
    }
}

我只添加3个新列Gebder,dob,ip。此代码显示此错误。它在添加这些新列之前工作。

问题是什么?

谢谢:)

1 个答案:

答案 0 :(得分:0)

可能是您将数据库中的Gender字段保留为null。要么允许插入null,要么允许使用Gender字段。