Huntario Huntario - 1 year ago 30
SQL Question

Setting up an if...elseif....else in PHP to validate email registration

Trying to tell the user that they have already registered if the email is a duplicate in the MySQL table. mysqli_connect_errno($link) prints '1062' this is the code for a duplicate primary key. Need some hints. The code above below "Thanks! You're already registered!" no matter if the email address is a duplicate or not.

<?php
$link = mysqli_connect("localhost", "mailpopper", "MAILpopPASSWORD", "mailpop");


// Ensure the connection is working

if($link === false){
die("Could not connect to database. Error: " . mysqli_connect_error());
}


// Escape user inputs for security

$first_name = mysqli_real_escape_string($link, $_POST['fname']);
$last_name = mysqli_real_escape_string($link, $_POST['lname']);
$org = mysqli_real_escape_string($link, $_POST['org']);
$email_address = mysqli_real_escape_string($link, $_POST['email']);

// attempt insert query execution

$sql = "INSERT INTO mail (fname, lname, org, email) VALUES ('$first_name', '$last_name','$org', '$email_address')";

// HERE IS WHERE I NEED HELP

$dupli = mysqli_errno($link);

if($dupli = 1062){

echo "Thanks! You're already registered!";
}

elseif(mysqli_query($link, $sql)){

echo "Success!";

} else{

echo "Error: record was not updated. Unable to post. Please try again or contact site owner with error: " . mysqli_error($link);

}

// close connection

mysqli_close($link);

?>


The code below works, but doesn't give a custom message when the user is already registered. If the user is already registered it says" "Error: record was not updated. Unable to post. Please try again or contact site owner with error: Duplicate entry 'Fagle@bibby.com' for key 'PRIMARY'
'

if(mysqli_query($link, $sql)){

echo "Success!";

else{

echo "Error: record was not updated. Unable to post. Please try again or contact site owner with error: " . mysqli_error($link);

}

// close connection

mysqli_close($link);

?>

Answer
if($dupli = 1062){

You are not using a comparison operator.

if($dupli === 1062){

Your snippets are a little confusing, is the middle one the one you want to use? Are you checking for the errno before executing your sql?

You are using the wrong function to fetch the error, use this one http://php.net/manual/en/mysqli.errno.php

Tried to fix your code:

$sql = "INSERT INTO mail (fname, lname, org, email) VALUES ('$first_name', '$last_name','$org', '$email_address')";
if(mysqli_query($link, $sql)){
    echo "Success!";
}else{
    $error = mysqli_errno($link);
    $errorMessage = "";
    switch ($error) {
        case 1062:
            $errorMessage = "Thanks! You're already registered!";
            break;
        default:
            $errorMessage = "Error: record was not updated. Unable to post. Please try again or contact site owner with error: " . mysqli_error($link);
            break;
    }
    echo $errorMessage;
}
  • execute the insert statement

    • returns true or false
  • if true, insert succeeded

  • if false:

    • fetch the error_code
    • put it in a switch construct to compare it to some codes you knows, define the message accordingly
    • (add messages easily)
    • define a default message for errorcodes you don't want to adress specifically
    • display message