PHP Form Post to e-mail and insert into MySQL

  CoreDuo 04:01 06 Jul 2006

I am currently using PHP to successfully post information from a form to my e-mail address. I Would also like the same script to Insert the information to a MySQL Database, What follows is my attempt to link them together :-


// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "[email protected]";
$Subject = "Website Enquiry";
$Name = Trim(stripslashes($_POST['Name']));
$Address = Trim(stripslashes($_POST['Address']));
$City = Trim(stripslashes($_POST['City']));
$County = Trim(stripslashes($_POST['County']));
$PostCode = Trim(stripslashes($_POST['PostCode']));
$HomeTel = Trim(stripslashes($_POST['HomeTel']));
$Mobile = Trim(stripslashes($_POST['Mobile']));
$Enquiry = Trim(stripslashes($_POST['Enquiry']));

// validation
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
print " meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "City: ";
$Body .= $City;
$Body .= "\n";
$Body .= "County: ";
$Body .= $County;
$Body .= "\n";
$Body .= "PostCode: ";
$Body .= $PostCode;
$Body .= "\n";
$Body .= "HomeTel: ";
$Body .= $HomeTel;
$Body .= "\n";
$Body .= "Mobile: ";
$Body .= $Mobile;
$Body .= "\n";
$Body .= "Enquiry: ";
$Body .= $Enquiry;
$Body .= "\n";

// open connection
$conn = mysql_connect("localhost","a2zcano","5755629");

// select dbase to use

// create sql statement
$sql = "INSERT INTO sitecontact ("$name","$address","$city","$county","$postcode","$hometel","$mobile","$enquiry")";

// execute sql statement
$result = mysql_query($sql,$conn) or die(mysql_error);

// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom>");

// redirect to success page
if ($success){
print " meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
print " meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

Its connecting to the Database but I am getting a parse error with my mysql statement. Any ideas? Thanks in advance.

  harristweed 08:04 06 Jul 2006

You need to tell MYSQL which fields to insert in to! Like this:

(assuming the database fields are the same names as your variables) and use single quotes.

// create sql statement
$sql = "INSERT INTO sitecontact name, address, city, county, postcde, hometel, mobile, enquiry VALUES ('$name','$address','$city','$county','$postcode','$hometel','$mobile",'$enquiry')";

// execute sql statement
$result = mysql_query($sql,$conn) or die(mysql_error);

  harristweed 08:06 06 Jul 2006

$sql = "INSERT INTO sitecontact (name, address, city, county, postcde, hometel, mobile, enquiry) VALUES ('$name','$address','$city','$county','$postcode','$hometel','$mobile','$enquiry')";

  CoreDuo 03:09 07 Jul 2006

As i followed the above another problem occurs, mysql error any thoughts guys?

  harristweed 08:16 07 Jul 2006

$result = mysql_query($sql,$conn) or die(mysql_error);

Is probably generating the error! Therefore try executing a simple statement.

$sql = "INSERT INTO sitecontact ( fieldname )VALUES ('fred')";

if that works build on it....

$sql = "INSERT INTO sitecontact ( fieldname )VALUES ('$name')";

$sql = "INSERT INTO sitecontact ( fieldname, fieldaddress )VALUES ('$name', '$address' )";

and so on.

These errors are usually a typo you have not seen or a missing ' or , in the wrong place!

  harristweed 08:24 07 Jul 2006

If the variables are from a posted form, why stripslashes?

$Name = Trim(stripslashes($_POST['Name']));

You only need stripslashes for data taken out of database. (If addslashes was used before the data was loaded into the database)

and that made me notice....

VALUES ('$name',

should be VALUES ('$Name',

...variables are case sensitive

$Name and $name are different .

Good luck!

  CoreDuo 22:03 07 Jul 2006

TY so much it hit me like a vision that I had never gotten before...

Kind Regards

  viper6277 21:25 17 Aug 2007

I'm a new member to this forum and I've been struggling with this task all day...I finally achieved my goal. so I wanted to post my people can use it to help email and insert into MySQL

This particular script also uses SMTP authentication.

// Connect to database
$hostname = "";
$username = "database";
$password = "password";
$dbname = "database";
$usertable = "tbl_service_requests";
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$equipmentid = $_REQUEST['equipmentid'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];

mysql_connect($hostname, $username, $password) or DIE("Unable to connect to MySQL server $hostname");
print "Connected to MySQL server<br>";
$selected = mysql_select_db($dbname) or DIE("Could not select requested db $dbname");

print "Connected to database $dbname<br>";

$query = "INSERT INTO tbl_service_requests (name, phone, equipment_id, email, problem)
VALUES ('$name','$phone','$equipmentid', '$email', '$message')";

$result = mysql_query($query) or DIE("Could not Execute Query on table $usertable");



$mail = new PHPMailer();

// Setup for outgoing mail server
$mail->Host = "";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username"; // SMTP username
$mail->Password = "password"; // SMTP password

$mail->From = "[email protected]";
$mail->FromName = "Service Form";
$mail->AddAddress("[email protected]", "Dispatch Department");
$mail->AddAddress($email = $_REQUEST['email']);

$mail->WordWrap = 100;

$body .= "Name: ";
$body .= $name = $_REQUEST['name'] ;
$body .= "<br>";
$body .= "Phone# : ";
$body .= $phone = $_REQUEST['phone'] ;
$body .= "<br>";
$body .= "Equipment ID : ";
$body .= $equipmentid = $_REQUEST['equipmentid'] ;
$body .= "<br>";
$body .= "eMail: ";
$body .= $email = $_REQUEST['email'] ;
$body .= "<br>";
$body .= "Comments: ";
$body .= $message = $_REQUEST['message'] ;

$mail->Subject = "Service Request";
$mail->Body = $body;
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;

header("Location: click here");


This thread is now locked and can not be replied to.

Elsewhere on IDG sites

Galaxy Note 8 vs iPhone X

Awful clip art from 1994 is being tweeted every hour by a bot

How to update iOS on iPhone or iPad

Les meilleures applications pour enfants 2017