我的PHP代码帮助中的类用户?

时间:2013-04-23 10:36:23

标签: php phpmyadmin

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

  

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

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

<?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();
            }
     }

 }

?>

1 个答案:

答案 0 :(得分: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();

更改其他查询......