cgchris99 Posted January 8, 2007 Share Posted January 8, 2007 I need to be able to test for a duplicate key during a record insert and display an error if this is the case.Can anyone help with this?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/33360-how-do-i-code-to-check-for-a-duplicate-key-on-mysql-insert/ Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 If it's a primary key it won't let you insert it. Quote Link to comment https://forums.phpfreaks.com/topic/33360-how-do-i-code-to-check-for-a-duplicate-key-on-mysql-insert/#findComment-155877 Share on other sites More sharing options...
Asheeown Posted January 8, 2007 Share Posted January 8, 2007 Before inserting the information use a select query, call the values you want to check and then compare them with the variables you already have defined. Then use an IF statement to check whether or not it's the same or not. If not then insert the information, if so return an error to the user. Quote Link to comment https://forums.phpfreaks.com/topic/33360-how-do-i-code-to-check-for-a-duplicate-key-on-mysql-insert/#findComment-155888 Share on other sites More sharing options...
HuggieBear Posted January 8, 2007 Share Posted January 8, 2007 [quote author=Fearsoldier link=topic=121531.msg499847#msg499847 date=1168272457]Before inserting the information use a select query, call the values you want to check and then compare them with the variables you already have defined.[/quote]A query is a good idea, but a comparison isn't. A row count would be easier in this situation...Example: A user enters their email address (unique column in the database)...[code]<?php$sql = "SELECT * FROM member_details WHERE email = '$user_entered_email_address'";$result = mysql_query($sql);if (mysql_num_rows($result) > 0){ // Sorry, the row exists, maybe display the form again}else { // The row doesn't exist, maybe put your insert code in here}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33360-how-do-i-code-to-check-for-a-duplicate-key-on-mysql-insert/#findComment-155896 Share on other sites More sharing options...
Asheeown Posted January 8, 2007 Share Posted January 8, 2007 True very true...more effiecient and less to type Quote Link to comment https://forums.phpfreaks.com/topic/33360-how-do-i-code-to-check-for-a-duplicate-key-on-mysql-insert/#findComment-155898 Share on other sites More sharing options...
cgchris99 Posted January 10, 2007 Author Share Posted January 10, 2007 thanks all. I will give it a shot and try it out. Quote Link to comment https://forums.phpfreaks.com/topic/33360-how-do-i-code-to-check-for-a-duplicate-key-on-mysql-insert/#findComment-157441 Share on other sites More sharing options...
ToonMariner Posted January 10, 2007 Share Posted January 10, 2007 if you are using keys then this process should never bee required - keys are there to help you link things together - every record in a table (where that record is a foreign key in another table) should have a uniquie key and as such you should use primary key with auto_increment. Quote Link to comment https://forums.phpfreaks.com/topic/33360-how-do-i-code-to-check-for-a-duplicate-key-on-mysql-insert/#findComment-157444 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.