how to check email already exist in database in php? – 5 Ways To Check if Email already exists Using PHP

how to check email already exist in database in php: every user get the simple solution for the how to check if an email address already exists in the database in php?

how to check email already exist in database in php

Validate the email if already exists. The validation of email exist is a restriction to the user for duplicate remove or deleted.

5 Ways To Check if Email already exists Using PHP

Set Email Column as UNIQUE on the Database

<?php
//email already exists validation in php
$con = mysqli_connect("localhost","root","","members");
if (isset($_POST['checkEmail'])) {
  $email = $_POST['email'];
  $insData = mysqli_query($con, "INSERT INTO `members`(`email`) VALUES('$email')") or die(mysqli_error($con));
}
?>
<form method="POST">
  Email:<br>
  <input type="email" name="email" required><br><br>
  <button name="checkEmail">Validate Email</button>
</form>

Validate Email using MySQL SELECT query with WHERE clause

Example – how to check data already exist in database in php

<?php
$con = mysqli_connect("localhost","root","","members");
if (isset($_POST['register'])) {
  $email = $_POST['email'];
  $sql = "SELECT * FROM members WHERE email='$email'";
  $res = mysqli_query($db, $sql);

  if(mysqli_num_rows($res) > 0){
    $email_error = "Sorry... email already taken";  
  }else{
    $query = mysqli_query($con, "INSERT INTO `members`(`email`) VALUES('$email')");
    echo 'Saved!';
  }
}

check username and email already exists

index.php

<?php
//php check if email address exists in database
$sql="select * from members where (membername='$membername' or email='$email');";

      $res=mysqli_query($mysqli,$sql);

      if (mysqli_num_rows($res) > 0) {
        
        $row = mysqli_fetch_assoc($res);
        if($email==isset($row['email']))
        {
            	echo "email already exists";
        }
		if($membername==isset($row['membername']))
		{
			echo "Sorry, membername  already exists";
		}
		}
else{
	
//do your insert code here or do something (run your code)
}

?>

Don’t Miss : php check file exists

Read Also:  Laravel Replicate Copy Table Row Examples

Check if username is already taken with PHP and MySQL

Create two files in your favorite text editor:

  1. register.php
  2. process.php
  3. styles.css

register.php:

<?php 
//email already exist validation in php
include('process.php') 

?>
<html>
<head>
  <title>Register</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <form method="post" action="register.php" id="signup_frm">
  	<h1>Register</h1>
  	<div <?php if (isset($name_error)): ?> class="userfrm_err" <?php endif ?> >
	  <input type="text" name="membername" placeholder="membername" value="<?php echo $membername; ?>">
	  <?php if (isset($name_error)): ?>
	  	<span><?php echo $name_error; ?></span>
	  <?php endif ?>
  	</div>
  	<div <?php if (isset($email_error)): ?> class="userfrm_err" <?php endif ?> >
      <input type="email" name="email" placeholder="Eneter Email" value="<?php echo $email; ?>">
      <?php if (isset($email_error)): ?>
      	<span><?php echo $email_error; ?></span>
      <?php endif ?>
  	</div>
  	<div>
  		<input type="password"  placeholder="Password" name="password">
  	</div>
  	<div>
  		<button type="submit" name="register" id="reg_btn">Sign Up</button>
  	</div>
  </form>
  /body>
</html>

styles.css
Now the styles.css file:

#signup_frm h1 {
  text-align: center;
}
body {
  background: #A9D9C3;
}
#signup_frm {
  width: 37%;
  margin: 100px auto;
  padding-bottom: 30px;
  border: 1px solid #918274;
  border-radius: 5px;
  background: white;
}
#signup_frm input {
  width: 80%;
  height: 35px;
  margin: 5px 10%;
  font-size: 1.1em;
  padding: 4px;
  font-size: .9em;
}
.userfrm_err span {
  width: 80%;
  height: 35px;
  margin: 3px 10%;
  font-size: 1.1em;
  color: #D83D5A;
}
.userfrm_err input {
  border: 1px solid #D83D5A;
}
#reg_btn {
  height: 35px;
  width: 80%;
  margin: 5px 10%;
  color: white;
  background: #3B5998;
  border: none;
  border-radius: 5px;
}

process.php

<?php 
//how to check if an email address already exists in the database in php
  $db = mysqli_connect('localhost', 'root', '', 'members_data');
  $membername = "";
  $email = "";
  if (isset($_POST['register'])) {
  	$membername = $_POST['membername'];
  	$email = $_POST['email'];
  	$password = $_POST['password'];

  	$sql_u = "SELECT * FROM members WHERE membername='$membername'";
  	$sql_e = "SELECT * FROM members WHERE email='$email'";
  	$res_u = mysqli_query($db, $sql_u);
  	$res_e = mysqli_query($db, $sql_e);

  	if (mysqli_num_rows($res_u) > 0) {
  	  $name_error = "Sorry... membername already taken"; 	
  	}else if(mysqli_num_rows($res_e) > 0){
  	  $email_error = "Sorry... email already taken"; 	
  	}else{
           $query = "INSERT INTO members (membername, email, password) 
      	    	  VALUES ('$membername', '$email', '".md5($password)."')";
           $results = mysqli_query($db, $query);
           echo 'Saved!';
           exit();
  	}
  }
?>

I hope you get an idea about how to check email already exist in database in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.