barkly Posted October 26, 2014 Share Posted October 26, 2014 (edited) Hi, I created a form that inserts first - last name, address, and email address into a mysql databse table. The record inserts correctly - No problems here. The problem is I only want to insert the data if the email and address doesn't exist. I set it up for just the email right now to try to get this to work but have failed. When the form is submitted and the record exists it inserts the record anyway and sends the email which it's not suppose to do. The form is suppose to after submitting - 1. validate 2. insert only if the record doesn't exist 3. send the email. Here's the code <?php $to = "email@email.com"; if(isset($_POST['submit'])) { // VALIDATION if(empty($_POST['firstName'])) { $errorfirstName .= "First Name Required"; } if(empty($_POST['lastName'])) { $errorfirstName .= "Last Name Required"; } if(empty($error)) # No error go ahead and email it. { $to = "$to"; $subject = 'the form'; $msg .="<html><head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <title>The Form</title> </head> <body> <table> <tr> <td>Email sent for confirmation</td> </tr> </table> </body> </html>"; $mail($to, $subject, $headers, $msg); if(!$result) { $error = "<div id='errors'>There was an unknown error </div>"; } else { include('connection.php'); $firstName = mysqli_real_escape_string($con, $_POST['firstName']); $lastName = mysqli_real_escape_string($con, $_POST['lastName']); $address = mysqli_real_escape_string($con, $_POST['address']); $email = mysqli_real_escape_string($con, $_POST['email']); $email = $_POST['email']; $sql = "SELECT * FROM table WHERE `email` = '{$email}'"; $result = mysql_query($sql); if ( mysql_num_rows ( $result ) > 0 ) { $error = "Email Exists."; } else { $error = "Email does not exist. Insert it!!!"; $sql="INSERT INTO table (firstName, lastName, address, email) VALUES ('$_POST[firstName]','$_POST[lastName]','$_POST[address]', '$_POST[email]')"; } if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } mysqli_close($con) { } } } } ?> <!-- Form --> <html> <head></head> <body> <section> <form method="POST" action="theform" name="for" onsubmit="return validateForm(this)"> <?php if(!empty($error)) { echo "$error"; } ?> This where the inputs would go not going to include them because not having an issue with the form </form> </section> </body> </html> Edited October 26, 2014 by barkly Quote Link to comment https://forums.phpfreaks.com/topic/292084-php-form-insert-mysql/ Share on other sites More sharing options...
MDCode Posted October 26, 2014 Share Posted October 26, 2014 (edited) You're mixing mysql with mysqli Edited October 26, 2014 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/292084-php-form-insert-mysql/#findComment-1494859 Share on other sites More sharing options...
barkly Posted October 27, 2014 Author Share Posted October 27, 2014 (edited) You're mixing mysql with mysqli Yes you're right!!! Thank You. I fixed that but now having problems with - $sql = "SELECT * FROM table WHERE `email` = '{$email}'"; I tried 'email' = '{$email}', 'address' = '{$address}'"; but it's inserting when the exact email and address exist. How can I code this to have muliple vars? Also I can enter emal@email and it validates and says it's not correct but it insert the data - the validation works but it doesn't stop the insert. Edited October 27, 2014 by barkly Quote Link to comment https://forums.phpfreaks.com/topic/292084-php-form-insert-mysql/#findComment-1494866 Share on other sites More sharing options...
barkly Posted October 27, 2014 Author Share Posted October 27, 2014 (edited) I fixed the email validation issue where it was validating because the address was missing .com but inserting the record anyway. The issues now - 1. If the record exists the error displays but email is sent anyway. 2. When I add address to - $sql = "SELECT * FROM table WHERE (`email` = '{$email}',`address` = '{$address}')"; the data is inserted anyway. Can someone help? Edited October 27, 2014 by barkly Quote Link to comment https://forums.phpfreaks.com/topic/292084-php-form-insert-mysql/#findComment-1494871 Share on other sites More sharing options...
Solution barkly Posted October 27, 2014 Author Solution Share Posted October 27, 2014 I got it to work. I coded it like `email` = '{$email}' OR `address` = '{$address}'"; I used OR in case one of the two were the same. The other issue I move the db code in a different area. Thanks SocialCloud for your help earlier. Quote Link to comment https://forums.phpfreaks.com/topic/292084-php-form-insert-mysql/#findComment-1494876 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.