Jump to content


Photo

Integrity constraint violation error

error sql mysql php database

Best Answer Hazukiy, 22 April 2013 - 11:10 AM

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.

Go to the full post


  • Please log in to reply
12 replies to this topic

#1 Hazukiy

Hazukiy

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts
  • LocationEngland
  • Age:19

Posted 18 April 2013 - 12:52 PM

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

 

 

 

 

 

 

 

 



#2 requinix

requinix

    Playful Member

  • Moderators
  • 5,742 posts
  • LocationWA

Posted 18 April 2013 - 12:55 PM

Where are you actually setting the value of $this->firstname?

#3 Hazukiy

Hazukiy

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts
  • LocationEngland
  • Age:19

Posted 18 April 2013 - 01:10 PM

Where are you actually setting the value of $this->firstname?

 

 

Uhhh I'm not too sure :/ 



#4 requinix

requinix

    Playful Member

  • Moderators
  • 5,742 posts
  • LocationWA

Posted 18 April 2013 - 01:40 PM

Because it seems like it's not happening.

#5 Hazukiy

Hazukiy

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts
  • LocationEngland
  • Age:19

Posted 18 April 2013 - 03:52 PM

Because it seems like it's not happening.

 

Which part?



#6 requinix

requinix

    Playful Member

  • Moderators
  • 5,742 posts
  • LocationWA

Posted 18 April 2013 - 04:10 PM

The part where you actually set the value of $this->firstname. Or $whatevervariable->firstname.

#7 Hazukiy

Hazukiy

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts
  • LocationEngland
  • Age:19

Posted 18 April 2013 - 05:49 PM

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



#8 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 18 April 2013 - 06:01 PM

So obviously you're not passing it any data.
You need to look at the code that implements this class.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#9 Hazukiy

Hazukiy

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts
  • LocationEngland
  • Age:19

Posted 19 April 2013 - 11:24 AM


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 by Hazukiy, 19 April 2013 - 11:31 AM.


#10 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 19 April 2013 - 11:48 AM

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 by Jessica, 19 April 2013 - 11:49 AM.

My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#11 Hazukiy

Hazukiy

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts
  • LocationEngland
  • Age:19

Posted 19 April 2013 - 12:18 PM

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;


#12 Hazukiy

Hazukiy

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts
  • LocationEngland
  • Age:19

Posted 22 April 2013 - 11:10 AM   Best Answer

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.



#13 RockStar7000

RockStar7000

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 07 December 2013 - 05:21 PM

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com