Page 1 of 1

Error: Duplicate entry '????????' for key 2

Posted: Tue Jun 16, 2009 10:35 am
by funkymeerkat
Hi, i set up a table for candidates where the unique identifer is the email address. When they fillout the form below, if that email address is currently in use, i want check the table and display an error such as "This email address is in use, please try another...."

Currently it just comes back as Error: Duplicate entry '???????" for key 2, which is going to mean nothing to the user.

Can someone given me some guidance please based on the below code.

Add To Table Form

Code: Select all

<form action="candidate_register_step2.php" method="post" name="candidate_register" id="candidate_register" onsubmit="return checkrequired(this); return emailcheck(this)">
        <table width="1000" border="0" cellspacing="3" cellpadding="3">
          <tr> 
            <td width="145"><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Firstname</font></td>
            <td width="834"><font size="2" face="Geneva, Arial, Helvetica, sans-serif"> 
              <input name="requiredfirstname" type="text" id="requiredfirstname" size="35" maxlength="80">
              </font></td>
          </tr>
          <tr> 
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Surname</font></td>
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif"> 
              <input name="requiredsurname" type="text" id="requiredsurname" size="35" maxlength="80">
              </font></td>
          </tr>
          <tr> 
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Date 
              of Birth</font></td>
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif">
              <input name="requireddob" type="text" id="requireddob" size="10" maxlength="10">
              <a href="javascript&#058;showCal('Calendar1')"><font color="#808080" size=1 face=verdana><b> 
              Select Date</b></font></a> <font size="1" face="Verdana, Arial, Helvetica, sans-serif">or 
              free type in dd/mm/yyyy format</font></font></td>
          </tr>
          <tr> 
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Current 
              Location </font></td>
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif"> 
              <select name="requiredlocation" id="requiredlocation">
                <option value="" selected>Select The Country</option>
                 <option value="GB">United Kingdom </option>              
</select>
              </font></td>
          </tr>
          <tr> 
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Country 
              of Birth</font></td>
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif"> 
              <select name="requiredbirth_country" id="requiredbirth_country">
                <option value="" selected>Select The Country</option>
                <option value="GB">United Kingdom </option>
              </select>
              </font></td>
          </tr>
          <tr> 
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Email</font></td>
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif"> 
              <input name="requiredemail" type="text" id="requiredemail" size="45" maxlength="120">
              </font></td>
          </tr>
          <tr> 
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Password</font></td>
            <td><font size="2" face="Geneva, Arial, Helvetica, sans-serif"> 
              <input name="requiredpassword" type="text" id="requiredpassword" size="8" maxlength="8">
              </font></td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            <td><input type="submit" name="Submit" value="Submit">
              <input name="Reset" type="reset" id="Reset" value="Reset"> </td>
          </tr>
        </table>
        
      </form>

Check Email & Added to Table Page

Code: Select all

<?php 
      
      
      
$firstname=$_POST['requiredfirstname'];
$surname=$_POST['requiredsurname'];
$dob=$_POST['requireddob'];
$location=$_POST['requiredlocation'];
$birth_country=$_POST['requiredbirth_country'];
$email=$_POST['requiredemail'];
$password=$_POST['requiredpassword'];
 
include("http://10.122.100.180/PaulTest/includes/login_details.inc");
 
mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error()); //Connects to database 
 
//echo $dbhost. "<br/>";
//echo $dbuser. "<br/>";
//echo $dbpass. "<br/>";
//echo $dbname. "<br/>";
 
mysql_select_db ($dbname) or die (mysql_error()); //Selects your database 
   
 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
$query = "INSERT INTO candidate (firstname, surname, dob, location, birth_country, email, password, status) 
VALUES ('$firstname', '$surname', '$dob', '$location', '$birth_country', '$email', '$password', 'Awaiting CV')";
 
//echo $query. "<br/>";
 
echo "";
 
$result = mysql_query($query) or die("Error: ". mysql_error());
 
?>
 
<form enctype="multipart/form-data" action="candidate_registration_complete.php" method="POST">
  <table width="1000" border="0" cellspacing="3" cellpadding="3">
    <tr> 
      <td width="126"><input type="hidden" name="MAX_FILE_SIZE" value="100000" />
              <font size="2" face="Geneva, Arial, Helvetica, sans-serif">Upload CV</font></td>
      <td width="853"><input name="uploadedfile" type="file" />
        <input name="id" type="hidden" id="id" value="<?php
 
$email=$_POST['requiredemail'];
 
include("includes/login.inc");
 
mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error()); //Connects to database 
 
mysql_select_db ($dbname) or die (mysql_error()); //Selects your database 
 
$sql = "SELECT * FROM candidate where email = '$email'";
 
 
 
    $result = mysql_query($sql);
    
    //echo $sql;
 
    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }
    
    if (mysql_num_rows($result) == 0) {
        echo "No Id";
 
    }
 
    // While a row of data exists, put that row in $row as an associative array
    // Note: If you're expecting just one row, no need to use a loop
    // Note: If you put extract($row); inside the following loop, you'll
    //       then create $userid, $fullname, and $userstatus
 while ($row = mysql_fetch_assoc($result)) {
 
    echo $row["id"];
 
    }
        
    mysql_free_result($result);
 
mysql_close();
 
 
?>

Re: Error: Duplicate entry '????????' for key 2

Posted: Tue Jun 16, 2009 9:01 pm
by yacahuma
very simple. echo mysql_errno and see the number you get. Now in your code just check for that error condition

Code: Select all

 
if (mysql_error() == XXXX) //XXXX is the number returned on duplicate
{
  echo "The email is already ...";
}
 

Re: Error: Duplicate entry '????????' for key 2

Posted: Thu Jun 18, 2009 11:58 am
by funkymeerkat
Hi thanks for your reply.

I am still however having difficulty. Where does this fit into my code?

Regards

Re: Error: Duplicate entry '????????' for key 2

Posted: Thu Jun 18, 2009 1:17 pm
by yacahuma
do not use the "or die()" I dont know why they always to that in the examples. You never want your code to die. You want to handle the error and show a meaningful message to your user.

So check you error just after executing the query

Re: Error: Duplicate entry '????????' for key 2

Posted: Thu Jun 18, 2009 2:11 pm
by funkymeerkat
Hi still struggling.

I get by but my basics are probably pretty poor. have only picked this php up by playing with scripts.

I have had a try and the place where i would assume to put it would be in red. Also the bit i have put in green - does it make sense?

Code: Select all

<?php 
 
$firstname=$_POST['requiredfirstname'];
$surname=$_POST['requiredsurname'];
$dob=$_POST['requireddob'];
$location=$_POST['requiredlocation'];
$birth_country=$_POST['requiredbirth_country'];
$email=$_POST['requiredemail'];
$password=$_POST['requiredpassword'];
 
include("http://10.122.100.180/PaulTest/includes/login_details.inc");
 
mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error()); //Connects to database 
 
//echo $dbhost. "<br/>";
//echo $dbuser. "<br/>";
//echo $dbpass. "<br/>";
//echo $dbname. "<br/>";
 
mysql_select_db ($dbname) or die (mysql_error()); //Selects your database 
                                                                                                                     
$query = "INSERT INTO candidate (firstname, surname, dob, location, birth_country, email, password, status) 
VALUES ('$firstname', '$surname', '$dob', '$location', '$birth_country', '$email', '$password', 'Awaiting CV')";
 
//echo $query. "<br/>";
 
$result = mysql_query($query) or die("Error: ". mysql_error());
 
[color=#FF0000]if (mysql_error() == [/color][color=#00BF00]Duplicate entry%[/color][color=#FF0000]) //XXXX is the number returned on duplicate
 
{
  
echo "The email is already ...";
 
}[/color]
 
?>
 
Thanks for your time on this.....

Re: Error: Duplicate entry '????????' for key 2

Posted: Thu Jun 18, 2009 2:32 pm
by yacahuma
no problem
change

Code: Select all

 
//change this
if (mysql_error() == Duplicate entry%) //XXXX is the number returned on duplicate
 
to this
echo "THE ERROR FOR DUPLICATE ENTRY IS" . mysql_error() ;
 

run you script and see what error do you get. Then knowing the error number add the line again

Code: Select all

 
   if (mysql_error() == 12345) //12345 is the number returned on duplicate, from your echo
   {
        //do whatever here
   }