PHP Form Post to e-mail and insert into MySQL

  CoreDuo 04:01 06 Jul 06
Locked

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 :-

?php

// 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
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
print " meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}

// 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
mysql_select_db("a2zcano_webcollect",$conn);

// 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\">";
}
else{
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 06

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 06

$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 06

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

  harristweed 08:16 07 Jul 06

$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 06

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 06

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

Kind Regards
Warren

  viper6277 21:25 17 Aug 07

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 code...so people can use it to help email and insert into MySQL

This particular script also uses SMTP authentication.

<?php
// Connect to database
$hostname = "database.hosting.earthlink.net";
$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");

mysql_close;

require("class.phpmailer.php5");

$mail = new PHPMailer();
$mail->IsSMTP();

// Setup for outgoing mail server
$mail->Host = "outgoing.mailserver.net";
$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;
$mail->IsHTML(true);

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

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

header("Location: click here");
exit();

?>

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

Nintendo Switch review: Hands-on with the intuitive modular console and its disappointing games…

1995-2015: How technology has changed the world in 20 years

VFX Oscar nominees 2017: Discover how the visual effects were created

Best alternatives to iTunes for Mac | Best music players for macOS: Free your music from the…