Skip to content
Advertisement

How to show PHP Duplicate Entry error message on current page

I am creating a signup form for a website I am developing. I am using PHP to insert the form into my database. I have the useremail set as a Unique Key in my database. When I type in an email that already exists in my database it brings me to a blank page with this

error message: Duplicate entry ” for key ’email_2′

How can I get this message to show up above the form, without clearing the form? In the code the “SERVER ERROR” and “BOTH FIELDS MUST BE COMPLETE” text is showing up in the correct place. How can I make the Duplicate entry error show up in the same way?

Here is the PHP code I currently have. Please note I am successfully connecting to my database, but did not include the code here.

$firstName  = $_POST['firstname'];
$lastName  = $_POST['lastname'];
$useremail  = $_POST['useremail'];
$password  = $_POST['password'];
$gender  = $_POST['gender'];
$birthday  = $_POST['birthday'];
$sqlDate = date('Y-m-d');

if (     $firstName!=''&&$lastName!=''&&$useremail!=''&&$password!=''&&$gender!=''&&$birthday!='')
{
   $sql = "INSERT INTO user (fname, lname, email, password, gender, birthday) VALUES ('$firstName', '$lastName', '$useremail', '$password', '$gender', '$birthday')";

   $res = mysql_query($sql) or die(mysql_error());
   if($res)
   {  

 header('location: http://myticapp.com/thankyou.php?useremail='.$useremail);
   }
   else
   {

 echo"<center><div class="denied">SERVER ERROR</div></center>";
   }

}
else if(!empty($_POST))
{
  echo"<center><div class="denied">BOTH FIELDS MUST BE COMPLETE</div>    </center>";
}

Here is the Form Code:

  `<form action="#" method="POST" class="signupform">
                        <input type="text" name="firstname" placeholder="First Name" required/>
                        <br>
                        <input type="text" name="lastname" placeholder="Last Name" required/>
                        <br>
                        <input type="text" name="useremail" placeholder="Email" required/>
                        <br>
                        <input type="text" name="password" placeholder="Password" required/>
                        <br>
                        <input type="text" name="gender" placeholder="Gender" required/>
                        <br>
                        <input type="text" name="birthday" placeholder="Birthday (YYYY-MM-DD)" required/>
                        <br>
                        <input type="submit" class="button buttonhover"/>
                    </form>`

Advertisement

Answer

You must run check query before inserting data.

<?
$firstName  = $_POST['firstname'];
$lastName  = $_POST['lastname'];
$useremail  = $_POST['useremail'];
$password  = $_POST['password'];
$gender  = $_POST['gender'];
$birthday  = $_POST['birthday'];
$sqlDate = date('Y-m-d');

$useremail = mysql_escape_string($useremail);
$duplicate = false;

try {
    $sql = "SELECT COUNT(*) as count FROM user WHERE email = '$useremail'";
    $res = mysql_query($sql)  or die(mysql_error());
    $data = mysql_fetch_assoc($res);
    if ($data['count'] > 0) {
        echo '<center>EMAIL ALREADY IN USE</center>';
        $duplicate = true;
    }
} catch (Exception $e) {

}

if (     $firstName!=''&&$lastName!=''&&$useremail!=''&&$password!=''&&$gender!=''&&$birthday!=''&&$duplicate==false)
{

   try
   {
        $sql = "INSERT INTO user (fname, lname, email, password, gender, birthday) VALUES ('$firstName', '$lastName', '$useremail', '$password', '$gender', '$birthday')";
        $res = mysql_query($sql)  or die(mysql_error());
    } 
    catch (Exception $e) 
    {
       echo "<center><div class="denied">",  $e->getMessage(), "</div></center>";

    }


  if(isset($res))
   {  
      header('location: http://myticapp.com/thankyou.php?useremail='.$useremail);
   }
   else
   {

 echo"<center><div class="denied">SERVER ERROR</div></center>";
   }

}
else if(!empty($_POST))
{
  echo"<center><div class="denied">BOTH FIELDS MUST BE COMPLETE</div>    </center>";
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement