How check username and email already exists in php


#1

How check username and email already exists in php . Below is what I am using-


$first = $_POST[‘first’];
$last = $_POST[‘last’];
$email = $_POST[‘email’];
$message = $_POST[‘message’];

date_default_timezone_set(‘Asia/Kolkata’);
$date=date(“d/m/Y h:i:sa”);

$sql = “INSERT INTO request (first,last,email,message,dt)
VALUES(’$first’,’$last’,’$email’,’$message’,’$date’)”;

if (mysqli_query($db, $sql)) {
echo “Request sent Sucessfully”;

  header ("Location: thankyou.html");

} else {
echo "Error: " . $sql . “
” . mysqli_error($db);
}
mysqli_close($db);

Any help please

Gary


#2

You don’t. What you do is set a unique index on the DB columns, attempt the insert and catch the duplicate error if any.


#3

Hi, Thanks I got it. Now need to try and display a error message on the form. Any help on this


#4

Post your code attempt.