Jump to content

Duplicate entry '0' for key 'PRIMARY'


rjcfan4ever

Recommended Posts

This is my error in my registration script: Duplicate entry '0' for key 'PRIMARY'

I did changned in phpmyadmin but he automaticly go's to 11  :shrug:

 

Here's the code:

<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>User Management System (Tom Cameron for NetTuts)</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
if(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
    $password = md5(mysql_real_escape_string($_POST['password']));
    $email = mysql_real_escape_string($_POST['email']);
    
 $checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'");
     
     if(mysql_num_rows($checkusername) == 1)
     {
     	echo "<h1>Error</h1>";
        echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
     }
     else
     {
     	$registerquery = mysql_query("INSERT INTO users (Username, Password, EmailAddress) VALUES('".$username."', '".$password."', '".$email."')") or die(mysql_error());
        if($registerquery)
        {
        	echo "<h1>Success</h1>";
        	echo "<p>Your account was successfully created. Please <a href=\"index2.php\">click here to login</a>.</p>";
        }
        else
        {
     		echo "<h1>Error</h1>";
        	echo "<p>Sorry, your registration failed. Please go back and try again.</p>";    
        }    	
     }
}
else
{
?>
    
   <h1>Register</h1>
    
   <p>Please enter your details below to register.</p>
    
<form method="post" action="register.php" name="registerform" id="registerform">
<fieldset>
	<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
	<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
        <label for="email">Email Address:</label><input type="text" name="email" id="email" /><br />
	<input type="submit" name="register" id="register" value="Register" />
</fieldset>
</form>
    
   <?php
}
?>
</div>
</body>
</html>

Link to comment
Share on other sites

your query should not be inserting a value of 0 for your primary key anyway...this leads me to believe that it is trying to insert invalid data into your primary key...what are your field names for that db table?

Link to comment
Share on other sites

i assuming that you also have it set to auto-increment...if so, use this query instead

 

$registerquery = mysql_query("INSERT INTO users VALUES('', '$username', '$password', '$email')") or die(mysql_error()); //blank field for the ID mysql field

Link to comment
Share on other sites

I believe it should be NULL

 

Make sure your id is set to auto_increment

 

You're better off with a query like

 

INSERT INTO `users` ( `username`, `password`, `email` ) VALUES ( '$username', '$password', '$email' )

Link to comment
Share on other sites

Still don't work

my code should work if you have the primary key field set to auto-increment.

 

I believe it should be NULL

 

Make sure your id is set to auto_increment

 

You're better off with a query like

 

INSERT INTO `users` ( `username`, `password`, `email` ) VALUES ( '$username', '$password', '$email' )

same query i wrote, different way...OP, backticks aren't needed since none of your filed names are mysql reserved words but they can be used

 

 

Link to comment
Share on other sites

<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>User Management System (Tom Cameron for NetTuts)</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
if(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
    $password = md5(mysql_real_escape_string($_POST['password']));
    $email = mysql_real_escape_string($_POST['email']);
    
 $checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'");
     
     if(mysql_num_rows($checkusername) == 1)
     {
     	echo "<h1>Error</h1>";
        echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
     }
     else
     {
     	$registerquery = mysql_query("INSERT INTO users VALUES('', '$username', '$password', '$email')") or die(mysql_error()); //blank field for the ID mysql field
        if($registerquery)
        {
        	echo "<h1>Success</h1>";
        	echo "<p>Your account was successfully created. Please <a href=\"index2.php\">click here to login</a>.</p>";
        }
        else
        {
     		echo "<h1>Error</h1>";
        	echo "<p>Sorry, your registration failed. Please go back and try again.</p>";    
        }    	
     }
}
else
{
?>
    
   <h1>Register</h1>
    
   <p>Please enter your details below to register.</p>
    
<form method="post" action="register.php" name="registerform" id="registerform">
<fieldset>
	<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
	<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
        <label for="email">Email Address:</label><input type="text" name="email" id="email" /><br />
	<input type="submit" name="register" id="register" value="Register" />
</fieldset>
</form>
    
   <?php
}
?>
</div>
</body>
</html>

Link to comment
Share on other sites

Well I hate to be the one to tell you this, but I just set up a database locally with the same structure as yours, and the above code, just copy/pasted, worked fine for me. No errors at all. Are you sure you're selecting the correct database when you call mysql_select_db()? Maybe you have another database with a similarly defined `users` table in it? That's the only thing that pops into my mind at the moment . . .

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.