deebler Posted July 8, 2011 Share Posted July 8, 2011 Hi - here is my php code for my Mysql table: <?php $con = mysql_connect("database"); if (!$con) { die('Could not connect: ' . mysql_error()); } $sql="INSERT INTO database (id, name, address, city, state, zip, cell, email,info) VALUES ('','$_POST[name]','$_POST[address]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[cell]','$_POST','$_POST[info]')"; mysql_select_db("tampa", $con); if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } mysql_close(); echo header ( "Location: /cc-common/contests"); // Your code here to handle a successful verification //}; ?> My question is what line of code can I put in to prevent duplicate emails AND addresses from being added to the database? I'm new to php and trying to figure it all out. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/ Share on other sites More sharing options...
AyKay47 Posted July 8, 2011 Share Posted July 8, 2011 your code needs revising <?php $con = mysql_connect("host","username","password"); //fill in your data if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database_name"); //insert appropriate data //declare variables $name = $_POST['name']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $cell = $_POST['cell']; $email = $_POST['email']; $info = $_POST['info']; $result = mysql_query("SELECT * FROM database WHERE email = '$email' AND address = '$address'") or exit(mysql_error()); //check for duplicates $num_rows = mysql_num_rows($result); //number of rows where duplicates exist if($num_rows == 0) { //if there are no duplicates...insert $sql="INSERT INTO database (id, name, address, city, state, zip, cell, email,info) VALUES ('','$_POST[name]','$_POST[address]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[cell]','$_POST[email]','$_POST[info]')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } } mysql_close(); header ("Location: /cc-common/contests"); // Your code here to handle a successful verification //}; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/#findComment-1240166 Share on other sites More sharing options...
deebler Posted July 8, 2011 Author Share Posted July 8, 2011 Wow thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/#findComment-1240175 Share on other sites More sharing options...
AyKay47 Posted July 8, 2011 Share Posted July 8, 2011 no problem, let me know if you have issues with it Quote Link to comment https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/#findComment-1240188 Share on other sites More sharing options...
deebler Posted July 8, 2011 Author Share Posted July 8, 2011 Just tested it out... it's still allowing duplicates to post to the database. Both email and address. Quote Link to comment https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/#findComment-1240196 Share on other sites More sharing options...
AyKay47 Posted July 8, 2011 Share Posted July 8, 2011 lets change the AND to an OR $result = mysql_query("SELECT * FROM database WHERE email = '$email' OR address = '$address'") or exit(mysql_error()); //check for duplicates I don't see how that would fail Quote Link to comment https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/#findComment-1240207 Share on other sites More sharing options...
deebler Posted July 8, 2011 Author Share Posted July 8, 2011 Ok, that appears to have done it! I just tested and it allowed me to submit the form, but never wrote another row in my table. Weird how the AND wouldn't work. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/#findComment-1240230 Share on other sites More sharing options...
AyKay47 Posted July 8, 2011 Share Posted July 8, 2011 well the AND would only work if you tried to submit the same address AND email, if you tried to submit the same email but a different address, it would still work....my error sorry about that..fixed now Quote Link to comment https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/#findComment-1240244 Share on other sites More sharing options...
deebler Posted July 8, 2011 Author Share Posted July 8, 2011 Makes sense, thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/#findComment-1240245 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.