title: php mysql + insert query errors
body: <p>i am creating a   page where users can edit their account and add some text   using php and mysql  . first i was able to do insert and update but now it display an error msg Duplicate entry  for key email address 
  how can i solve this problem
   this is the code of the account setting but it do not contains any email address field   so i will display all be cause i do not know where is the desired code to be displayed  </p>
  
  <p>i edit the code maybe this one is producing the error ??</p>
  
  <h1> register .php</h1>
  
  <pre><code>   &lt;?php require_once('for members/scripts/connect.php'); ?&gt;
  
  
  &lt;?php
  ob_start();
   function countryQuery(){
  
    $countryData = mysql_query("SELECT * FROM country") or die("could select database");
  
    while($record = mysql_fetch_array($countryData)){
  
       echo'&lt;option value="' . $record['country_name'] .  '"&gt;' . $record['country_name'] . '&lt;/option&gt;';
  
    }
  
  }
  
  
  function specializationQuery(){
  
  $specData = mysql_query("SELECT * FROM specialization");
  
    while($recordJob = mysql_fetch_array($specData)){
  
       echo'&lt;option value="' . $recordJob['specialization_name'] .  '"&gt;' . $recordJob['specialization_name'] . '&lt;/option&gt;';
  
    }
  
  
  }
  
  function districtQuery(){
  
  $distData = mysql_query("SELECT * FROM districts");
  
    while($recorddist = mysql_fetch_array($distData)){
  
       echo'&lt;option value="' . $recorddist['district_name'] .  '"&gt;' . $recorddist['district_name'] . '&lt;/option&gt;';
  
    }
  
  
  }
  
  function villageQuery(){
  
  $villageData = mysql_query("SELECT * FROM village");
  
    while($recordvillage = mysql_fetch_array($villageData)){
  
       echo'&lt;option value="' . $recordvillage['village_name'] .  '"&gt;' . $recordvillage['village_name'] . '&lt;/option&gt;';
  
    }
  
  
  }
  
  
  
   //default value
  $message = "Fields Marcked with an [*] are Required";
    $username = "";
    $fname = "";
    $lname = "";
    $specialization = "";
    $email = "";
    $pass1 = "";
    $pass2 = "";
    $district = "";
    $village = "";
  
  if(isset($_POST['username'])){
    $username = $_POST['username'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $pass1 = $_POST['pass1'];
    $pass2 = $_POST['pass2'];
    $bdate = $_POST['birthdate'];
    $phone = $_POST['phonenumber'];
    $country = $_POST['country'];
    //$local_adress = $_POST['adress'];
    $specialization = $_POST['specialization'];
    $district = $_POST['district'];
    $village = $_POST['village'];
  
    //error handeling
    if((!$username)||(!$fname)||(!$lname)||(!$email)||(!$pass1)||(!$pass2)||(!$specialization)||(!$district)||(!$village)){
    $message = "**** Please insert the Required Fields below ****&lt;br /&gt;";
  
    if($fname == "")
   {
       $message = $message . "Enter First name&lt;br/&gt;";
   }
    if($lname == "")
   {
       $message = $message . "Enter Last name&lt;br/&gt;";
   }
  
   if($specialization == 0)
   {
       $message = $message . "Select Your Job&lt;br /&gt;";
   }
   if($district == 0)
   {
       $message = $message . "Select Your District&lt;br /&gt;";
   }
   if($village == 0)
   {
       $message = $message . "Select Your Village&lt;br /&gt;";
   }
   if($email == "")
   {
       $message = $message . "Enter Email Adress&lt;br/&gt;";
   }
    if ($username == "") {
      $message = $message . "Enter User Name&lt;br/&gt;";
    }
  
    if($pass1 == "")
   {
       $message = $message . "Enter password&lt;br/&gt;";
   }
    if($pass2 == "")
   {
       $message = $message . "rechek the password &lt;br/&gt;";
   }
  
   } 
  
     else if($pass1!=$pass2){
     $message = "your password do not match!";
     }else{
     //securing the data
     $username = preg_replace("#[^0-9a-z]#i","",$username);
     $fname = preg_replace("#[^0-9a-z]#i","",$fname);
     $lname = preg_replace("#[^0-9a-z]#i","",$lname);
     //$pass1 = sha1($pass1);
  
     $email = mysql_real_escape_string($email);
     // checking for duplicate
     $user_query = mysql_query("SELECT user_name FROM user WHERE user_name = '$username'LIMIT 1") or die("could not check the username");
     $count_username = mysql_num_rows($user_query);
  
      $email_query = mysql_query("SELECT email_address FROM user WHERE email_address = '$email'LIMIT 1") or die("could not check the email");
     $count_email = mysql_num_rows($email_query);
  
      if($count_username &gt; 0){
       $message = " your username is alredy in use";
      }elseif($count_email &gt; 0){
        $message = "your email is alredy in use";
      }else{
  
        $query = mysql_query("INSERT INTO user(user_name, first_name, last_name, address, district_name, village_name, birth_date, email_address, specialization_name, password, registered_date)VALUES('$username', '$fname', '$lname', '$country', '$district', '$village', '$bdate', '$email', '$specialization', '$pass1',  now())")or die("could not insert data");  
  
      $message = "you have  now been registered";
      //from the social website
  
      $getid = mysql_fetch_array($query);
              $_SESSION['user_id'] = $getid['user_rid'];
              $_SESSION['login'] = 'true';
              $_SESSION['login_user'] = $getid['username'];
      header("Location: home.php");
      }
     }
   }
  
  
  
  
  
  ?&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Register Page&lt;/title&gt;
  &lt;link href="style/stylesheet.css" rel="stylesheet" type="text/css" /&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  
  
  &lt;table width="200" border="0" align="center"&gt;
  &lt;tr&gt;
    &lt;tr&gt;
      &lt;td&gt;
      &lt;div id="visitorHeader"&gt;&lt;/div&gt;
      &lt;img src="web_header copy.jpg" alt="visitor header" /&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
       &lt;td&gt;
         &lt;h2 class="registerTitle"&gt;Registration Fields&lt;/h2&gt;
         &lt;h4 class="registerTitle"&gt;Sign Up Today....&lt;/h4&gt;
   &lt;!-- &lt;div class="container center"&gt; --&gt;
         &lt;p style="color:#FF0000" align="center"&gt;&lt;?php print("$message")?&gt;&lt;/p&gt;
        &lt;/td&gt;
  
      &lt;tr&gt;
          &lt;table width="680" border="0" align="center"&gt;
          &lt;form action="register.php" method="post"&gt;
  
  
  
           &lt;td&gt; 
          &lt;/span&gt;&lt;label for="firstname"&gt;&lt;span class="Fields"&gt;First Name&lt;/span&gt; &lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
          &lt;td&gt;
          &lt;input type="text" name="fname" placeholder="Firstname" /&gt;&lt;/td&gt;
  
        &lt;td&gt;&lt;span class="Fields"&gt;Last Name&lt;/span&gt;&lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
  
        &lt;td&gt;&lt;input type="text" name="lname" placeholder="Lastname" /&gt;&lt;/td&gt;
  
  
  
      &lt;tr&gt;
        &lt;td&gt;&lt;label for="birthdate" class="Fields"&gt;Birth Date                     &lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="date" name="birthdate" value= "YYYY_MM_DD"  onfocus="if (this.value == 'YYYY_MM_DD') {this.value = '';}" onblur="if (this.value == '') {this.value = 'YYYY_MM_DD';}" /&gt;
          &lt;br /&gt;&lt;/td&gt;
        &lt;td class="Fields"&gt;&lt;label for="phonenumber"&gt;Phone Number&lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="number" name="phonenumber" value="000-0-000 000" onfocus="if (this.value == '000-0-000 000') {this.value = '';}" onblur="if (this.value == '') {this.value = '000-0-000 000';}" /&gt;&lt;/td&gt;
  
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td class="Fields"&gt;&lt;label for="country"&gt;Country&lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;select name="country"  class="select"&gt;
        &lt;option value="0"&gt;-- Select Your Country --&lt;/option&gt;
          &lt;?php countryQuery(); ?&gt;
        &lt;/select&gt;&lt;/td&gt;
        &lt;td class="Fields"&gt;&lt;label for="specialisation"&gt;Specialisation &lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;select name="specialization"  class="select"&gt;
        &lt;option value="0"&gt;-- Select Your Specialization --&lt;/option&gt;
  
          &lt;?php specializationQuery(); ?&gt;
        &lt;/select&gt;&lt;/td&gt;
  
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td class="Fields"&gt;&lt;label for="district"&gt;District&lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;select name="district"  class="select"&gt;
        &lt;option value="0"&gt;-- Select Your District --&lt;/option&gt;
  
          &lt;?php districtQuery(); ?&gt;
        &lt;/select&gt;&lt;/td&gt;
  
        &lt;td class="Fields"&gt;&lt;label for="village"&gt;Village&lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;select name="village"  class="select"&gt;
        &lt;option value="0"&gt;-- Select Your Village --&lt;/option&gt;
  
          &lt;?php villageQuery(); ?&gt;
        &lt;/select&gt;&lt;/td&gt;
  
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td class="Fields"&gt;&lt;label for="email"&gt;Email Adress&lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="text" name="email" placeholder="Email Adress" /&gt;
          &lt;br /&gt;&lt;/td&gt;
        &lt;td&gt;&lt;label for="username"&gt;&lt;span class="Fields"&gt;User Name&lt;/span&gt; &lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="text" name="username" placeholder="Username" /&gt;
          &lt;br /&gt;&lt;/td&gt;
  
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td class="Fields"&gt;&lt;label for="password"&gt;Password&lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="password" name="pass1" placeholder="Password" /&gt;
          &lt;br /&gt;&lt;/td&gt;
        &lt;td class="Fields"&gt;&lt;label for="password2"&gt;Re_Password&lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="password" name="pass2" placeholder="Validate Password" /&gt;
          &lt;br /&gt;&lt;/td&gt;
  
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="submit" value="Register"/&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
    &lt;/form&gt;
  &lt;/table&gt;
  &lt;!--&lt;/div&gt;--&gt;
  
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;?php require_once('footer.php'); ?&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tr&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php ob_end_flush(); ?&gt;
  </code></pre>