Got a "duplicate entry for key '1'" message
Posted: Wed Jun 14, 2006 11:47 pm
I got an error message which is " Duplicate entry ''studentID" for key 1" when I click the submit button to send the information from the HTML form into the database. I have checked in the database to find out if there is any duplicate entry for the same studentID in the table, but I found NONE. Everytime I want to insert the value from form to the database, I got the same message, even though there is no duplicate entry in the table. However, when I cheked the database, the values that I just want to insert have been inserted in the database, despite the error message of duplicate entry that I received. Here, I use the StudentID value from the input user gives as the primary key.
Is there any way that I should do to avoid from getting the message again? here is the codes I use to insert the information from the form into the database, namely "register.php".
this is the connection file that I used, namely "connect.php"
How to make the message does not appear again? I really appreciate any help from all of you.
Thank you.
Is there any way that I should do to avoid from getting the message again? here is the codes I use to insert the information from the form into the database, namely "register.php".
Code: Select all
<?php
include ("connect.php");
if (isset($_POST['Proceed'])) {
submitData();
}
function submitData(){
global $database;
global $table;
global $errCon;
mysql_select_db($database) or die($errCon . mysql_error());
//check user exist or not
$studID = $_POST['requiredstudentID'];
$sql="SELECT studentID FROM $table WHERE studentID='$studID'";
$result=mysql_query($sql);
if ($result && mysql_numrows($result) > 0) {
echo 'user already exists. Please click the Back button at your browser to register';
} else {
//declaration of variables of the input from user
$name = $_POST['requiredname'];
$pword = $_POST['pw1'];
$programme = $_POST['requiredprogramme'];
$year = $_POST['requiredyear'];
$sem = $_POST['requiredsem'];
$gender = $_POST['requiredgender'];
//Insert input into database
$query = "INSERT INTO $table (Name,StudentID,Password,Programme,Year, Semester, Gender) VALUES ('$name', '$studID','$pword','$programme', '$year', '$sem','$gender') ";
$result = mysql_query($query);
if (!mysql_query($query)){
print (mysql_error());
}
else {
//echo "Data Inserted!";
?>
<script>
window.location="http://localhost/login_poo.php";
</script>
<?
}
}
}
?>this is the connection file that I used, namely "connect.php"
Code: Select all
<?php
//<!-- DATABASE CONNECTION INFO---->
$errCon = "<br> Contact your webmaster. <br>";
$hostname="localhost";
$mysql_login="root";
$mysql_password="";
$database="myfyp";
$table= "useraccount";
//&dbtype="MySQL";
// connect to the database server
mysql_connect($hostname, $mysql_login, $mysql_password)
or die($errCon . mysql_error());
// select a database
mysql_select_db($database) or die($errCon . mysql_error());
?>Thank you.