php/mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • W051
    New Member
    • Mar 2012
    • 5

    php/mysql

    I get the error of Duplicate entry '' for key 1 when running a mysql query
    this comes from a form that has textboxes and then tries to save to student table in mysql.

    Code:
    $id=mysql_real_escape_string($_POST['txtstudentid']);
    $password=mysql_real_escape_string($_POST['txtpassword']);
    $dob=mysql_real_escape_string($_POST['txtdob']);
    $firstname=mysql_real_escape_string($_POST['txtfirstname']);
    $lastname=mysql_real_escape_string($_POST['txtlastname']);
    $house=mysql_real_escape_string($_POST['txthouse']);
    $town=mysql_real_escape_string($_POST['txttown']);
    $county=mysql_real_escape_string($_POST['txtcounty']);
    $country=mysql_real_escape_string($_POST['txtcountry']);
    $postcode=mysql_real_escape_string($_POST['txtpostcode']);
    
    
    
        // Build an sql statment to add the student details
      	$sql="INSERT INTO student(studentid,password,dob,firstname,lastname,house,town,county,country,postcode) VALUES('$id','$password','$dob','$firstname','$lastname','$house','$town','$county','$country','$postcode')";
    	$result = mysql_query($sql,$conn);
        	if($result){ 
      echo"<br/>Your details have been updated"; 
      echo "<BR>"; 
      echo "<a href='Home.html'>Back to main page</a>"; 
    } else { 
      echo "There has been an error <br/>"; 
      print mysql_error(); 
    } 
    
    
    // close connection 
    mysql_close($conn);
    ?>
    Last edited by Rabbit; Mar 6 '12, 11:39 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The error is saying that you're trying to insert an id that already exists in the table.

    Comment

    • W051
      New Member
      • Mar 2012
      • 5

      #3
      Originally posted by Rabbit
      The error is saying that you're trying to insert an id that already exists in the table.
      actually, i tried to add different ids into the table but i still get the error message

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Are you sure? Did you output the string to check?

        Comment

        • W051
          New Member
          • Mar 2012
          • 5

          #5
          Originally posted by Rabbit
          Are you sure? Did you output the string to check?
          yes, if it helps i read somewhere that it might be to do with an outdated version of php? I am using Notepad to write my php code.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            That may be the case, but I doubt it. Please post the output of the SQL string that causes the error.

            Comment

            • W051
              New Member
              • Mar 2012
              • 5

              #7
              Originally posted by Rabbit
              That may be the case, but I doubt it. Please post the output of the SQL string that causes the error.
              thanks for your help, but i have it sorted now, by adding this to where i insert the data into the form. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

              Comment

              Working...