rjcfan4ever Posted July 6, 2011 Share Posted July 6, 2011 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 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> Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/ Share on other sites More sharing options...
AyKay47 Posted July 6, 2011 Share Posted July 6, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239232 Share on other sites More sharing options...
rjcfan4ever Posted July 6, 2011 Author Share Posted July 6, 2011 UserID int(11) <- the problem Premary key Username varchar(65) Password varchar(32) EmailAddress varchar(255) Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239235 Share on other sites More sharing options...
AyKay47 Posted July 6, 2011 Share Posted July 6, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239244 Share on other sites More sharing options...
rjcfan4ever Posted July 6, 2011 Author Share Posted July 6, 2011 Still don't work Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239250 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 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' ) Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239253 Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2011 Share Posted July 6, 2011 Is there a new error message? Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239256 Share on other sites More sharing options...
AyKay47 Posted July 6, 2011 Share Posted July 6, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239261 Share on other sites More sharing options...
rjcfan4ever Posted July 6, 2011 Author Share Posted July 6, 2011 I have done everything what you said.. But still the same error Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239269 Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2011 Share Posted July 6, 2011 You never answered whether the UserId field is actually set as an auto_increment type or not. Is it? Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239271 Share on other sites More sharing options...
rjcfan4ever Posted July 6, 2011 Author Share Posted July 6, 2011 UserID is set in auto_incremen Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239279 Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2011 Share Posted July 6, 2011 OK, post your current code and let's see if we can't get to the bottom of this. Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239290 Share on other sites More sharing options...
rjcfan4ever Posted July 6, 2011 Author Share Posted July 6, 2011 <?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> Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239302 Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2011 Share Posted July 6, 2011 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 . . . Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239308 Share on other sites More sharing options...
jcbones Posted July 6, 2011 Share Posted July 6, 2011 Show us a DB dump of your table structure. Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239325 Share on other sites More sharing options...
AyKay47 Posted July 7, 2011 Share Posted July 7, 2011 I find it hard to believe that if you are properly debugging both your mysql connection and your db selection that you would not receive any errors in your script Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239362 Share on other sites More sharing options...
rjcfan4ever Posted July 7, 2011 Author Share Posted July 7, 2011 I used the database of my hosting (Awardspace) Quote Link to comment https://forums.phpfreaks.com/topic/241255-duplicate-entry-0-for-key-primary/#findComment-1239569 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.