vendredi 24 juin 2016

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); ?>

Aucun commentaire:

Enregistrer un commentaire