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

时间:2016-03-19 08:06:37

标签: php mysql pdo

它表示' display_name'不能为空,但是当我将 $ display_name = null; 更改为$ display_name =''; 时,我填写的其他字段将会更新到数据库但在display_name字段中它没有保存。那么有人可以帮我解决吗?谢谢!

    <?php

class Users {

    public $display_name = null;
    public $email = null;
    public $password = null;
    public $salt = "Zo4rU5Z1YyKJAASY0PT6EUg7BBYdlEhPaNLuxAwU8lqu1ElzHv0Ri7EM6irpx5w";





public function __construct( $data = array() ) {
               if( isset( $data['email']) ) $this->email = stripslashes( strip_tags( $data['email'] ) );
              if( isset( $data['password'] ) ) $this->password = stripslashes( strip_tags( $data['password'] ) );
}

public function storeFormValues( $params ) {
              //store the parameters
              $this->__construct( $params );
}

public function userLogin() {
                   //success variable will be used to return if the login was successful or not.
                   $success = false;
                  try{
                     //create our pdo object
                     $con = new PDO( db_dsn, server_user, server_pass );
                     //set how pdo will handle errors
                     $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                     //this would be our query.
                     $sql = "SELECT * FROM gs_users_table WHERE email = :email AND password = :password LIMIT 1";
                      //prepare the statements
                     $stmt = $con->prepare( $sql );
                     //give value to named parameter :email
                     $stmt->bindValue( "email", $this->email, PDO::PARAM_STR );
                     //give value to named parameter :password
                     $stmt->bindValue( "password", hash("sha512", $this->password . $this->salt), PDO::PARAM_STR );
                     $stmt->execute();

                     $valid = $stmt->fetchColumn();

                    if( $valid ) {
                          $success = true;
                     }

                     $con = null;
                     return $success;
                 }catch (PDOException $e) {
                      echo $e->getMessage();
                      return $success;
                 }
}

public function signup() {
     $correct = false;
     try {
              $con = new PDO( db_dsn, server_user, server_pass );
              $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
              $sql = "INSERT INTO gs_users_table (display_name, email, password) VALUES (:display_name, :email, :password)";
              $stmt = $con->prepare( $sql );
              $stmt->bindValue( "display_name", $this->display_name, PDO::PARAM_STR );
              $stmt->bindValue( "email", $this->email, PDO::PARAM_STR );
              $stmt->bindValue( "password", hash("sha512", $this->password . $this->salt), PDO::PARAM_STR );
              $stmt->execute();
              return "Sign Up Successful <br/> <a href='login.php'>Login Now!</a>";
       }catch( PDOException $e ) {
                 return $e->getMessage();
       }
}
}


?>

1 个答案:

答案 0 :(得分:0)

根据评论,听起来问题是您没有设置$this->display_name而不是:

$valid = $stmt->fetchColumn();

if( $valid ) {
    $success = true;
}

尝试:

$valid = $stmt->fetchAll();

if( $valid ) {
    $success = true;
    $this->display_name = $valid[0]['display_name'];
}