Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076572
Share on other sites

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";
}

Link to comment
https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076582
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/205736-primary-key-error/#findComment-1076671
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.