bradkenyon Posted August 12, 2008 Share Posted August 12, 2008 I have this code that adds email addresses to a table, and I want to check to see if the email address is already in the table, how would I go about that w/ the current code? Any help is appreciated. include('db.php'); if($_POST) { $fname=$HTTP_POST_VARS['fname']; $lname=$HTTP_POST_VARS['lname']; $email=$HTTP_POST_VARS['email']; $query = mysql_query("INSERT INTO maillist (fname, lname, email) VALUES('$fname', '$lname', '$email')"); result - mysql_query($query); if($result) echo mysql_affected_rows().' maillist inserted into database.'; ?> <h2 align="center">Thank You for signing up, you'll now receive festival updates!</h2> <? } else { ?> <span class="heading">Sign Up</span> <p> <form method="post" action="<?=$_SERVER["PHP_SELF"]?>"> <table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#CAD7BB"> <tr> <td><strong>Email:</strong></td> <td><input type="text" name="email"></td> </tr> <tr> <td><strong>First Name:</strong></td> <td><input type="text" name="fname"></td> </tr> <tr> <td><strong>Last Name:</strong></td> <td><input type="text" name="lname"></td> </tr> <tr> <td colspan="2"><input type="Submit" name="Submit" value="Submit"></td> </tr> </table> <? } ?> Quote Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted August 12, 2008 Share Posted August 12, 2008 Try this, it checks for an matching email address. include('db.php'); if($_POST) { $fname=$HTTP_POST_VARS['fname']; $lname=$HTTP_POST_VARS['lname']; $email=$HTTP_POST_VARS['email']; $checktable = mysql_query("SELECT * FROM maillist WHERE email='$email' "); $nrows = mysql_num_rows($checktable); if ($nrows == 0) { $query = mysql_query("INSERT INTO maillist (fname, lname, email) VALUES('$fname', '$lname', '$email')"); result - mysql_query($query); if($result) echo mysql_affected_rows().' maillist inserted into database.'; ?> <h2 align="center">Thank You for signing up, you'll now receive festival updates!</h2> <? } } else { ?> <span class="heading">Sign Up</span> <p> <form method="post" action="<?=$_SERVER["PHP_SELF"]?>"> <table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#CAD7BB"> <tr> <td><strong>Email:</strong></td> <td><input type="text" name="email"></td> </tr> <tr> <td><strong>First Name:</strong></td> <td><input type="text" name="fname"></td> </tr> <tr> <td><strong>Last Name:</strong></td> <td><input type="text" name="lname"></td> </tr> <tr> <td colspan="2"><input type="Submit" name="Submit" value="Submit"></td> </tr> </table> <? } ?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2008 Share Posted August 12, 2008 it is much faster to use COUNT(), although problemHelpPlease's solution should still work: $nrows = mysql_result(mysql_query("SELECT COUNT(*) FROM maillist WHERE email='$email'"), 0, 0); keep in mind this won't play well if you end up with a query error of any kind. Quote Link to comment Share on other sites More sharing options...
discomatt Posted August 12, 2008 Share Posted August 12, 2008 Rather than perform two queries, I like to make the column 'unique' in MySQL... you can then check mysql_error() for the string 'Duplicate entry' -> If it's there, you know that the email is already taken. I'm also an efficiency freak, though Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2008 Share Posted August 12, 2008 making the column a unique key is also a good way to add a last line of defense against duplicate entries. i would argue that queries are never the bottleneck in small systems (vs. doing a string search), but i don't think it's really relevant - disco's right, you could do it with one query alone. Quote Link to comment Share on other sites More sharing options...
bradkenyon Posted August 12, 2008 Author Share Posted August 12, 2008 thanks guy, did the trick! Quote Link to comment 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.