Hazukiy Posted April 18, 2013 Share Posted April 18, 2013 Hi, I'm in the middle of constructing my registration and login form and I keep getting this error when I try to process the login. Here's the error I'm getting: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstname' cannot be null Here's the PHP code: <?php class Users { public $firstname = null; public $lastname = null; public $username = null; public $email = null; 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['password'] ) ) $this->password = stripslashes( strip_tags( $data['password'] ) ); } public function storeFormValues( $params ) { $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 members 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 members(firstname, lastname, username, email, password) VALUES(:firstname, :lastname, :username, :email, :password)"; $stmt = $con->prepare( $sql ); $stmt->bindValue( "firstname", $this->firstname, PDO::PARAM_STR ); $stmt->bindValue( "lastname", $this->lastname, PDO::PARAM_STR ); $stmt->bindValue( "username", $this->username, 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(); } } } ?> My database layout is as follows Field Type Null Default Comments user_id int(11) No firstname varchar(50) No lastname varchar(50) No username varchar(50) No email varchar(50) No password var(250) No Indexes: Keyname Type Cardinality Field PRIMARY PRIMARY 0 user_id username UNIQUE 0 username email UNIQUE 0 email Quote Link to comment Share on other sites More sharing options...
requinix Posted April 18, 2013 Share Posted April 18, 2013 Where are you actually setting the value of $this->firstname? Quote Link to comment Share on other sites More sharing options...
Hazukiy Posted April 18, 2013 Author Share Posted April 18, 2013 Where are you actually setting the value of $this->firstname? Uhhh I'm not too sure :/ Quote Link to comment Share on other sites More sharing options...
requinix Posted April 18, 2013 Share Posted April 18, 2013 Because it seems like it's not happening. Quote Link to comment Share on other sites More sharing options...
Hazukiy Posted April 18, 2013 Author Share Posted April 18, 2013 Because it seems like it's not happening. Which part? Quote Link to comment Share on other sites More sharing options...
requinix Posted April 18, 2013 Share Posted April 18, 2013 The part where you actually set the value of $this->firstname. Or $whatevervariable->firstname. Quote Link to comment Share on other sites More sharing options...
Hazukiy Posted April 18, 2013 Author Share Posted April 18, 2013 The part where you actually set the value of $this->firstname. Or $whatevervariable->firstname. I've recently changed a few things, here's the latest code: <?php class Users { public $firstname = null; public $lastname = null; public $username = null; public $email = null; public $password = null; public $salt = "Zo4rU5Z1YyKJAASY0PT6EUg7BBYdlEhPaNLuxAwU8lqu1ElzHv0Ri7EM6irpx5w"; public function __construct( $data = array() ) { if( isset( $data['firstname'] ) ) $this->firstname = stripslashes( strip_tags( $data['firstname'] ) ); if( isset( $data['lastname'] ) ) $this->lastname = stripslashes( strip_tags( $data['lastname'] ) ); if( isset( $data['username'] ) ) $this->username = stripslashes( strip_tags( $data['username'] ) ); 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 ) { $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 members 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 members(firstname, lastname, username, email, password) VALUES(:firstname, :lastname, :username, :email, :password)"; $stmt = $con->prepare( $sql ); $stmt->bindValue( "firstname", $this->firstname, PDO::PARAM_STR ); $stmt->bindValue( "lastname", $this->lastname, PDO::PARAM_STR ); $stmt->bindValue( "username", $this->username, 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(); } } } ?> As you can see the $this variable should now work but it's still coming up with the same error. If I set my database structure to allow NULL the sign up works in the sense that it adds something to the database but of course it's just adding 'NULL' for everything. It's a bit weird this one, I've never come across this kind of error before ^.- Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 18, 2013 Share Posted April 18, 2013 So obviously you're not passing it any data. You need to look at the code that implements this class. Quote Link to comment Share on other sites More sharing options...
Hazukiy Posted April 19, 2013 Author Share Posted April 19, 2013 (edited) So obviously you're not passing it any data.You need to look at the code that implements this class. Ah sorry forgot to add this, it goes in the register.php.This goes at the top of the Register.php <?php if( !(isset( $_POST['register'] ) ) ) { ?> This goes at the bottom of the Register.php <?php } else { $usr = new Users; $usr->storeFormValues( $_POST ); if( $_POST['rpassword'] == $_POST['cpassword'] ) { echo $usr->register($_POST); } else { echo "Password and Confirm password not match"; } } ?> Edited April 19, 2013 by Hazukiy Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 19, 2013 Share Posted April 19, 2013 (edited) So, you are expecting to get the data in your constructor. You wrote __construct($data). Yet you instantiate the class this way: $usr = new Users; So... I see your second function which is kind of silly to bother doing that. Obviously something isn't working in the process. Start debugging by printing the values at each point and see where you've lost the data. Edited April 19, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
Hazukiy Posted April 19, 2013 Author Share Posted April 19, 2013 So, you are expecting to get the data in your constructor. You wrote __construct($data). Yet you instantiate the class this way: $usr = new Users; So... I see your second function which is kind of silly to bother doing that. Obviously something isn't working in the process. Start debugging by printing the values at each point and see where you've lost the data. Oh no I meant I just forgot to add it originally. Would this "cannot be null" error that I'm getting, wouldn't that be something to do with this as I'm declaring the variables as null and in the database it's set to not null? (Doing some troubleshooting on it now) public $firstname = null; public $lastname = null; public $username = null; public $email = null; public $password = null; Quote Link to comment Share on other sites More sharing options...
Solution Hazukiy Posted April 22, 2013 Author Solution Share Posted April 22, 2013 I've now fixed the issue. It was because I forgot to add the data in and I also forgot to name the input forms. Quote Link to comment Share on other sites More sharing options...
RockStar7000 Posted December 7, 2013 Share Posted December 7, 2013 Can you please explain how you fixed the error? I'm having the same issue and I don't understand what you mean by "I forgot to add the data in and I also forgot to name the input forms." Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.