0

我的类文件夹中有以下代码。当我执行它时,我收到以下错误

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

. 我认为问题出在我的代码的第一部分?是变量的名称吗?

<?php

 class Users {
     public $username = null;
     public $firstname;
     public $lastname;
     public $firmname;
     public $email;
     public $password = null;
     public $salt = "Zo4rU5Z1YyKJAASY0PT6EUg7BBYdlEhPaNLuxAwU8lqu1ElzHv0Ri7EM6irpx5w";

     public function __construct( $data = array() ) {
         if( isset( $data['username'] ) ) $this->username = stripslashes( strip_tags( $data['username'] ) );
         if( isset( $data['firstname'] ) ) $this->firstname = stripslashes( strip_tags( $data['lastname'] ) );
         if( isset( $data['lastname'] ) ) $this->lastname = stripslashes( strip_tags( $data['username'] ) );
         if( isset( $data['firmname'] ) ) $this->firmname = stripslashes( strip_tags( $data['firmname'] ) );
         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 = false;
         try{
            $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); 
            $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            $sql = "SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1";

            $stmt = $con->prepare( $sql );
            $stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
            $stmt->bindValue( "password", hash("sha256", $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 register() {

        $correct = false;
            try {
                $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
                $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                $sql = "INSERT INTO users(username, firstname, lastname, firmname, email, password, date ) VALUES(:username, :firstname, :lastname, :firmname, :email, :password, NOW())";

                $stmt = $con->prepare( $sql );
                $stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
                $stmt->bindValue( "firstname", $this->firstname, PDO::PARAM_STR );
                $stmt->bindValue( "lastname", $this->lastname, PDO::PARAM_STR );
                $stmt->bindValue( "firmname", $this->firmname, PDO::PARAM_STR );
                $stmt->bindValue( "email", $this->email, PDO::PARAM_STR );
                $stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
                $stmt->execute();
                return "Registration Successful <br/> <a href='index.php'>Login Now</a>";
            }catch( PDOException $e ) {
                return $e->getMessage();
            }
     }

 }

?>
4

1 回答 1

0

你忘了:这里之前的变量

$stmt->bindValue( ":username", $this->username, PDO::PARAM_STR );
$stmt->bindValue( ":firstname", $this->firstname, PDO::PARAM_STR );
$stmt->bindValue( ":lastname", $this->lastname, PDO::PARAM_STR );
$stmt->bindValue( ":firmname", $this->firmname, PDO::PARAM_STR );
$stmt->bindValue( ":email", $this->email, PDO::PARAM_STR );
$stmt->bindValue( ":password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
$stmt->execute();

更改其他查询以及...

于 2013-04-23T10:43:48.227 回答