Jump to content

having trouble sending mail and adding to MySQL database at the same time!


slaterino

Recommended Posts

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 = "info@myhost.com";

        $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");
        }
    }

 

Link to comment
Share on other sites

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')");

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 = "info@thghosting.co.uk";

        $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>";
            }
        ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.