ballouta Posted June 2, 2009 Share Posted June 2, 2009 Hello, I have a query hidden in the "Send to a friend' page, that collects email addresses and insert them into a table with a primary key 'email' but if this user has sent before (to a friend) using the same email address, the insertion code will view a duplication problem, how i can prevent this? Do I check if the email address is already exists before inserting it? or I can hide the warning? my code is: <?php $query="INSERT INTO `send_to` (`email` ,`name`) VALUES ( '$email' , '$name')"; $query_result = mysql_query ($query) or die("Problem with the query: <br>" . @mysql_error()); ?> Thank alot Link to comment https://forums.phpfreaks.com/topic/160636-duplicate-entry/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 2, 2009 Share Posted June 2, 2009 Using or die() is primary intended for troubleshooting why a query fails. Because a duplicate key INSERT causes the query to fail, using or die() in your situation will both cause your code to output the error message and cause your code to stop execution. You need to test if the query worked or failed (using an if(){}else{} conditional test instead of using or die()) and if it failed with a duplicate key error (you can use the mysql_errno() to test which error occurred), simply continue with normal execution of your code. You could also use ON DUPLICATE KEY UPDATE with your INSERT query to update a dummy column, such as a timestamp of when the user caused an email to be sent. This would avoid the generation of the duplicate key error message. You can also use the IGNORE keyword to cause a warning instead of a fatal error. Link to comment https://forums.phpfreaks.com/topic/160636-duplicate-entry/#findComment-847750 Share on other sites More sharing options...
ballouta Posted June 2, 2009 Author Share Posted June 2, 2009 Thanks for the explanation it is working now Link to comment https://forums.phpfreaks.com/topic/160636-duplicate-entry/#findComment-847758 Share on other sites More sharing options...
deepson2 Posted December 7, 2009 Share Posted December 7, 2009 I am looking for something really similar, bello is the some solution which i didn't get the proper idea what does this mean? You need to test if the query worked or failed (using an if(){}else{} conditional test instead of using or die()) and if it failed with a duplicate key error (you can use the mysql_errno() to test which error occurred), simply continue with normal execution of your code. You could also use ON DUPLICATE KEY UPDATE with your INSERT query to update a dummy column, such as a timestamp of when the user caused an email to be sent. This would avoid the generation of the duplicate key error message. Can anyone please explain me with some example that how can i prevent the database from inserting duplicate entries? Link to comment https://forums.phpfreaks.com/topic/160636-duplicate-entry/#findComment-972712 Share on other sites More sharing options...
deepson2 Posted December 7, 2009 Share Posted December 7, 2009 Three methods for preventing database from duplicate insertion- 1) select * from table where name ='$name' AND address= '$address' AND phone ='$phone' { // dont insert }else { //insert } 2)Check the session value with the database value(?) $_SESSION['name'] = $name select * from table where name ='$name' AND address= '$address' AND phone ='$phone' { // dont insert }else { //insert } 3)with mysql - INSERT ... ON DUPLICATE KEY UPDATE ( ???) which method is the best? can anyone point me in a right direction? Link to comment https://forums.phpfreaks.com/topic/160636-duplicate-entry/#findComment-972747 Share on other sites More sharing options...
deepson2 Posted December 8, 2009 Share Posted December 8, 2009 Any help? Link to comment https://forums.phpfreaks.com/topic/160636-duplicate-entry/#findComment-973224 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.