Got a "duplicate entry for key '1'" message

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
thania
Forum Newbie
Posts: 11
Joined: Sun Mar 12, 2006 9:19 pm

Got a "duplicate entry for key '1'" message

Post by thania »

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".

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()); 

?>
How to make the message does not appear again? I really appreciate any help from all of you.
Thank you.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Either Change studentID to an auto_increment field in the database table and exclude it from your query, or modify it so that it is not a primary key.
thania
Forum Newbie
Posts: 11
Joined: Sun Mar 12, 2006 9:19 pm

Post by thania »

I cannot make the StudentID an auto-increment field since every user has their own unique studentID. I actually have tried to make the StudentID NOT A PRIMARY KEY by inserting another field named Index, which the value is auto-incremented, and also the PRIMARY KEY. However, when I did that, another problem occurs, which there is double entries of the values I just submitted in the database. I dont know why this happens. Besides, when I delete one of the double entries in the database, the auto-increment value is not automatically refreshed, makes the values in the INDEX field like 'jumping' and not continous. I have tried to make the auto-increment value = 1 in the sql query as what I have referred, which is to make it auto-refreshed. However, this results in an error in the table. For your information, I am using PhpMyAdmin to create my database included in the EasyPHP package. Can you please help me with this? I am really stucked.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

It is either still a Primary KEY or you have it set to UNIQUE.

Might want to try something like..

Code: Select all

ALTER TABLE `mytable` DROP INDEX `studentID`
Post Reply