ilikephp Posted June 24, 2010 Share Posted June 24, 2010 Hi, I have a php form that is working fine, but when duplicate entries are entered I get this code on a new page: "Duplicate entry '2010-111-3' for key 'PRIMARY'" How can it be displayed on the same page using echo method? Thank you, Quote Link to comment https://forums.phpfreaks.com/topic/205736-primary-key-error/ Share on other sites More sharing options...
JonnoTheDev Posted June 24, 2010 Share Posted June 24, 2010 If a database field is a unique primary key it cannot accept duplicate entries. i.e You cannot have a users table with two users having an id (primary key) of 1. You must not attempt to insert records containing a key that is already in use. Use an auto-increment on the primary key, or generate the key before each insert. Quote Link to comment https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076572 Share on other sites More sharing options...
ilikephp Posted June 24, 2010 Author Share Posted June 24, 2010 Yeah I know that it can not be inserted in my database, But I need to let the user knows that "There is duplicate key enter new information" using the if method Thank you... Quote Link to comment https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076576 Share on other sites More sharing options...
Mchl Posted June 24, 2010 Share Posted June 24, 2010 Why are you using a date (?) as a primary key? You should not allow users to enter primary key by hand. Use mysql_errno to catch this error and display proper message to user. Quote Link to comment https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076580 Share on other sites More sharing options...
JonnoTheDev Posted June 24, 2010 Share Posted June 24, 2010 Yeah I know that it can not be inserted in my database, But I need to let the user knows that "There is duplicate key enter new information" using the if method Thank you... Then you must run a SELECT query prior to an INSERT. $x = mysql_query("SELECT id FROM users WHERE name='Neil Johnson'"); if(mysql_num_rows($x)) { print "You are already in my database"; } else { mysql_query("INSERT INTO users SET name='Neil Johnson'"); print "You have been added to my database"; } Quote Link to comment https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076582 Share on other sites More sharing options...
ilikephp Posted June 24, 2010 Author Share Posted June 24, 2010 usually if there is an error when filling a text box: for example: "Max. characters should be 5" How can it be displayed using echo in top of the form? Quote Link to comment https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076588 Share on other sites More sharing options...
kenrbnsn Posted June 24, 2010 Share Posted June 24, 2010 For that you probably need to use AJAX. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076637 Share on other sites More sharing options...
JonnoTheDev Posted June 24, 2010 Share Posted June 24, 2010 For that you probably need to use AJAX. No. When the form is submitted, validate the data. Capture any errors in lets say an array. If there are errors simply re-display the same page with the errors next to the form or wherever you want to. Only when the form validates successfully should you save the data and redirect the user to a success screen. This is basic stuff. I would strongly recommend going through a few tutorials if you are struggling. http://www.google.co.uk/search?q=php+form+validation+tutorial Quote Link to comment https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076671 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.