Duplicate entry

I have a registration form, One of the fields in it is email.
I checked phpmyadmin and noticed I made email a unique index
I entered a copy of an email and nothing seemed to haooen (shouldn’t the catch block be run (wouldnt the error code b e 2300)

		try {
		 
		$sql = "INSERT INTO users (
		  name,
		  email,
		  avatar,
		  ip,
		  password,
		  display
		  ) VALUES (
		  :name,
		  :email,
		  NULL,
		  :ip,
		  :password,
		  1
		  )";
		  
		$statement = $conn->prepare($sql);
		
		$parameters = array(
			':name' => $name,
			':email' => $email,
			':password' => $password,
			':ip' => $user_ip,
		  );
		
		$statement->execute($parameters);
		
		
		echo "SQL: ".$sql;
		echo '<pre>';print_r($parameters);echo '</pre>';
		

		//require('success.php');
		
		} catch(PDOException $e) {
		
		   if ($e->getCode() == '2300') {
					  require('duplicate_email.php');
		   }
    	}

So I double checked the SQL query & the parameters

and get

SQL: INSERT INTO users ( name, email, avatar, ip, password, display ) VALUES ( :name, :email, NULL, :ip, :password, 1 )

Array
(
    [:name] => test
    [:email] => xxx@gmail.com
    [:password] => test
    [:ip] => 107.218.57.40
)

(Edited by gandalf458 to hide personal email address.)

Make sure you set PDO in exception mode, by default it’s off

Also, the particular code could be different, better to check it beforehand

1 Like

Post the DDL of your DB

what is DDL?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.