Jump to content

Integrity constraint violation error


Hazukiy

Recommended Posts

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

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/277120-integrity-constraint-violation-error/
Share on other sites

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 ^.-

 

 


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"; 
}
}
?>
 

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.

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;
  • 7 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.