slaterino Posted October 17, 2008 Share Posted October 17, 2008 Hey, I have created a contact form that as well as sending a mail to me will also add the contact details to a MySQL database. I've got it working so that it sends the e-mail but can't seem to get it adding to the database at all! Can someone have a quick look at the script below and let me know if there is anything obvious that may be causing this problem? Thanks! Russ if(isset($_POST['docontact'])) { $to = "[email protected]"; $def_subject = "Enrolment"; $min_title_len = 2; $min_firstname_len = 2; $min_lastname_len = 2; $min_email_len = 6; if( isset($_POST['first_name']) and strlen($_POST['first_name']) >= $min_firstname_len and isset($_POST['last_name']) and strlen($_POST['last_name']) >= $min_lastname_len and isset($_POST['email']) and preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $_POST['email']) ) { $subject = $_POST['title'] . " " . $_POST['first_name'] . " " . $_POST['last_name'] . " enrolled onto " . $_POST['txt_coursename']; $message = $_POST['last_name'] ."\n==================================================\n" .$_POST['first_name'] ." | " .$_POST['email']; $header = "From: " .$_POST['first_name'] ." <" .$_POST['email'] .">\r\n"; mail($to, $subject, $message, $headers); mysql_query("INSERT INTO enrol (`title`,`first_name`,`last_name`) VALUES ('$en_title','$en_first','$en_last')"); header("location: payment.php"); } else { header("location: ?" .$_SERVER['QUERY_STRING'] ."&fillall"); } } Quote Link to comment https://forums.phpfreaks.com/topic/128899-having-trouble-sending-mail-and-adding-to-mysql-database-at-the-same-time/ Share on other sites More sharing options...
rhodesa Posted October 17, 2008 Share Posted October 17, 2008 Where are you setting the values of $en_title, $en_first and $en_last? i would change mysql_query("INSERT INTO enrol (`title`,`first_name`,`last_name`) VALUES ('$en_title','$en_first','$en_last')"); to $en_title = mysql_real_escape_string($_POST['title']); $en_first = mysql_real_escape_string($_POST['first_name']); $en_last = mysql_real_escape_string($_POST['last_name']); mysql_query("INSERT INTO enrol (`title`,`first_name`,`last_name`) VALUES ('$en_title','$en_first','$en_last')"); Quote Link to comment https://forums.phpfreaks.com/topic/128899-having-trouble-sending-mail-and-adding-to-mysql-database-at-the-same-time/#findComment-668250 Share on other sites More sharing options...
slaterino Posted October 17, 2008 Author Share Posted October 17, 2008 Hi, Made the changes mentioned and also added or die(mysql_error()) to the end of the last line. The error message I'm now getting is: Unknown column 'title' in 'field list' What will this error refer to? I've not had one like this before. I've double-checked that the 'title' field is entered correctly in the form, and it is. If anyone can help that would be great! Quote Link to comment https://forums.phpfreaks.com/topic/128899-having-trouble-sending-mail-and-adding-to-mysql-database-at-the-same-time/#findComment-668261 Share on other sites More sharing options...
Acs Posted October 17, 2008 Share Posted October 17, 2008 Try removing the ` ` from the field's list enrol (title,first_name,last_name) Quote Link to comment https://forums.phpfreaks.com/topic/128899-having-trouble-sending-mail-and-adding-to-mysql-database-at-the-same-time/#findComment-668272 Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 That means that there is no column called 'title' in your table. Column names are case sensitive. Do you have it capitalized in your table? Quote Link to comment https://forums.phpfreaks.com/topic/128899-having-trouble-sending-mail-and-adding-to-mysql-database-at-the-same-time/#findComment-668282 Share on other sites More sharing options...
slaterino Posted October 17, 2008 Author Share Posted October 17, 2008 Yeah, I realised the error of my ways and have now altered my script thus: <PHP> $en_title = mysql_real_escape_string($_POST['title']); $en_first = mysql_real_escape_string($_POST['first_name']); $en_last = mysql_real_escape_string($_POST['last_name']); mysql_query("INSERT INTO enrol (en_title, en_first, en_last) VALUES ('$en_title','$en_first','$en_last')") or die(mysql_error()); </PHP> In my enrol table there are fields called en_title, en_first and en_last. When I run the code now it is not outputting anything at all. I get the html around the php but nothing where the content should be. This one is really puzzling me! Quote Link to comment https://forums.phpfreaks.com/topic/128899-having-trouble-sending-mail-and-adding-to-mysql-database-at-the-same-time/#findComment-668311 Share on other sites More sharing options...
slaterino Posted October 17, 2008 Author Share Posted October 17, 2008 Yes, The database is now being populated and e-mails are being sent! This is going in the right direction. I had to make a few minor changes to script and so on to get this thing working and am now finding that once the form has been submitted instead of going to the headers as it was doing it is now just reloading the form page with this error above the form: <PHP> Warning: Cannot modify header information - headers already sent by (output started at /home/sites/mydomain.com/public_html/dive/includes/nav.php:29) in /home/sites/mydomain.com/public_html/dive/enrol-process.php on line 47 </PHP> Line 29 of nav.php has nothing special on it apart from a link. Line 47 in enrol-process.php is the header link for successful forms, but it was working before so I am slightly confused. I've included the enrol-process.php script again below with it's new changes. If the user doesn't fill in all the fields I want to echo a sentence in the form asking for them to do that and I've included a short piece of the script that I was hoping to do that with. Again, if anyone can help with this I will be eternally grateful! <?php if(isset($_POST['docontact'])) { $to = "[email protected]"; $def_subject = "Enrolment"; $min_title_len = 2; $min_firstname_len = 2; $min_lastname_len = 2; $min_email_len = 6; if( isset($_POST['first_name']) and strlen($_POST['first_name']) >= $min_firstname_len and isset($_POST['last_name']) and strlen($_POST['last_name']) >= $min_lastname_len and isset($_POST['email']) and preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $_POST['email']) ) { $subject = $_POST['title'] . " " . $_POST['first_name'] . " " . $_POST['last_name'] . " enrolled onto " . $_POST['txt_coursename']; $message = $_POST['last_name'] ."\n==================================================\n" .$_POST['first_name'] ." | " .$_POST['email']; $headers = "From: " .$_POST['first_name'] ." <" .$_POST['email'] .">\r\n"; mail($to, $subject, $message, $headers); $en_title = mysql_real_escape_string($_POST['title']); $en_first = mysql_real_escape_string($_POST['first_name']); $en_last = mysql_real_escape_string($_POST['last_name']); mysql_query("INSERT INTO enrol (en_title, en_first, en_last) VALUES ('$en_title','$en_first','$en_last')") or die(mysql_error()); header("location: payment.php"); } else { header("location: ?" .$_SERVER['QUERY_STRING'] ."&fillall"); } } ?> <form action="" method="post"> <input type="hidden" name="docontact" value="1" /> <?php if(isset($_GET['fillall'])) { echo "<p class=\"error\">Please fill out all mandatory fields. This error may also occur if your email address is invalid.</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/128899-having-trouble-sending-mail-and-adding-to-mysql-database-at-the-same-time/#findComment-668326 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.