nathanmaxsonadil Posted July 30, 2007 Share Posted July 30, 2007 I have this code to add the logged in user's email to the mysql database echo "<br/><ul><li><a href='stixy.php?insert=1'>Add your self to the list</a></li></ul>"; if(isset($_GET['insert']) && $_GET['insert'] == '1'){ $sql = "INSERT INTO emailists (stixy) VALUES ('" . $session->userinfo['email'] . "')"; if($sqlres = mysql_query($sql)){ echo 'email address added'; }else{ echo mysql_error(); } } the only problem is that you can add you email twice Is there any way so that if you've already added your email it does not add it again? Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/ Share on other sites More sharing options...
Vizor Posted July 30, 2007 Share Posted July 30, 2007 http://www.sqlteam.com/article/using-exists Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311478 Share on other sites More sharing options...
hitman6003 Posted July 30, 2007 Share Posted July 30, 2007 Either do a query to check for it already being in the table, or add a unique index to your table and catch the error letting the user know that they have already been added. Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311479 Share on other sites More sharing options...
trq Posted July 30, 2007 Share Posted July 30, 2007 <?php echo "<br/><ul><li><a href='stixy.php?insert=1'>Add your self to the list</a></li></ul>"; if(isset($_GET['insert']) && $_GET['insert'] == 1) { $sql = "SELECT stixy FROM emaillist WHERE stixy = '{$session->userinfo['email']}'"; if ($result = mysql_query($sql)) { if (!mysql_num_rows($result)) { $sql = "INSERT INTO emailists (stixy) VALUES ('" . $session->userinfo['email'] . "')"; if ($sqlres = mysql_query($sql)){ echo 'email address added'; } else { echo mysql_error(); } } else { echo "email address already exists"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311481 Share on other sites More sharing options...
hitman6003 Posted July 30, 2007 Share Posted July 30, 2007 You could also use "ON DUPLICATE KEY UPDATE"... http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311484 Share on other sites More sharing options...
nathanmaxsonadil Posted July 31, 2007 Author Share Posted July 31, 2007 it does not add anything even if your email address is not yet added (using thrope's code) Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311506 Share on other sites More sharing options...
trq Posted July 31, 2007 Share Posted July 31, 2007 Check for errors. I spelt your field name differently. <?php echo "<br/><ul><li><a href='stixy.php?insert=1'>Add your self to the list</a></li></ul>"; if(isset($_GET['insert']) && $_GET['insert'] == 1) { $sql = "SELECT stixy FROM emailists WHERE stixy = '{$session->userinfo['email']}'"; if ($result = mysql_query($sql)) { if (!mysql_num_rows($result)) { $sql = "INSERT INTO emailists (stixy) VALUES ('" . $session->userinfo['email'] . "')"; if ($sqlres = mysql_query($sql)){ echo 'email address added'; } else { echo mysql_error(); } } else { echo "email address already exists"; } } else { echo mysql_error(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311521 Share on other sites More sharing options...
nathanmaxsonadil Posted July 31, 2007 Author Share Posted July 31, 2007 thanks Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311523 Share on other sites More sharing options...
nathanmaxsonadil Posted July 31, 2007 Author Share Posted July 31, 2007 one more question is there a way so that the add youself to the list button changes when you are on the list and instead says remove myself from the list and removes you? Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311527 Share on other sites More sharing options...
trq Posted July 31, 2007 Share Posted July 31, 2007 Of course there is. You wanna try writting some code? Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311529 Share on other sites More sharing options...
nathanmaxsonadil Posted July 31, 2007 Author Share Posted July 31, 2007 I wrote the code to delete it but I dont know how to change the add yourself to the list button dynamicly Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311535 Share on other sites More sharing options...
nathanmaxsonadil Posted July 31, 2007 Author Share Posted July 31, 2007 OK I figured this all out Quote Link to comment https://forums.phpfreaks.com/topic/62580-solved-double-adding/#findComment-311585 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.