我试图从提交到电子邮件地址的表单输入字段发送数据,同时表单数据被插入到数据库表中。我已成功地设法将表单数据提交到数据库中,我现在正努力将表单数据作为电子邮件发送。这是我的下面的PHP代码。所有的PHP代码都在一个文件中。我试图将数据发送到的电子邮件地址不是Gmail,Hotmail等电子邮件,它是公司自己的邮寄地址。发送申请表并提交并插入到数据库中
<?php
$conn = mysqli_connect("", "", "", ""); --> deleted this because of this post
if (!$conn) {
die ("Connection failed: ". mysqli_connect_error());
}
$course = $_POST ['course'];
$firstname = $_POST ['firstname'];
$surname = $_POST ['surname'];
$DOB = $_POST ['DOB'];
$gender = $_POST ['gender'];
$address1 = $_POST ['address1'];
$address2 = $_POST ['address2'];
$city = $_POST ['city'];
$postcode = $_POST ['postcode'];
$mobile = $_POST ['mobile'];
$home = $_POST ['home'];
$email = $_POST ['email'];
$residency = $_POST ['residency'];
$learning = $_POST ['learning'];
$qualifications = $_POST ['qualifications'];
$sql = "INSERT INTO form (course, firstname, surname, DOB, gender, address1, address2, city, postcode, mobile, home, email, residency, learning, qualifications) VALUES ('$course', '$firstname', '$surname', '$DOB', '$gender', '$address1', '$address2', '$city', '$postcode', '$mobile', '$home', '$email', '$residency', '$learning', '$qualifications')";
$result = mysqli_query($conn, $sql);
以上是代码来获取表单数据到数据库中。下面的代码是我尝试将信息发送到一个电子邮件地址。
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if(
$_POST["course"]==""|| $_POST["firstname"]==""||
$_POST["surname"]==""||
$_POST["DOB"]==""||
$_POST["gender"]==""||
$_POST["address1"]==""||
$_POST["address2"]==""||
$_POST["city"]==""||
$_POST["postcode"]==""||
$_POST["mobile"]==""||
$_POST["home"]==""||
$_POST["email"]==""||
$_POST["residency"]==""||
$_POST["learning"]==""||
$_POST["qualifications"]=="")
{
echo "Please ensure all fields are filled in.";
} else {
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Email";
}
else{
$course = $_POST['course'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$DOB = $_POST['DOB'];
$gender = $_POST['gender'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$postcode = $_POST['postcode'];
$mobile = $_POST['mobile'];
$home = $_POST['home'];
$email = $_POST['email'];
$residency = $_POST['residency'];
$learning = $_POST['learning'];
$qualifications = $_POST['qualifications'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
// Send Mail By PHP Mail Function
mail("[email protected]", $course, $firstname, $surname, $DOB, $gender, $address1, $address2, $city, $postcode, $mobile, $home, $email, $residency, $learning, $qualifications);
echo "Your mail has been sent successfuly! We will get back to as soon as we can!";
}
}
}
header("Location: apply.php");
以下是只是在我的HMTL标签的第一部分:
<form class="form" id="contact_form" action="submitApplication.php" method="post">
提交按钮:
<button type="submit" name="submit" class="btn btn-primary btn-lg outline hvr-grow">Submit Application</button>
一些补充,当我尝试提交申请将上面的所有代码都放在一个文件中,在提交时,没有数据被发送到数据库,同时在同一个文件中使用电子邮件的代码。如果我删除同一文件中的电子邮件的代码,则将申请表提交给数据库。我必须尽可能简洁,并希望有人能帮助我!谢谢!
这表明你在电子邮件代码语法错误。打开PHP错误报告并查看那里显示的内容。 –
此外,您的代码易受SQL注入的影响。请使用mysqli –
准备好的语句[Little Bobby](http://bobby-tables.com/)表示*** [您的脚本存在SQL注入攻击风险。](http://stackoverflow.com/questions/) 60174/how-can-i-prevent-sql -injection-in-php)***了解[MySQLi](http:/ /)的[prepared](http://en.wikipedia.org/wiki/Prepared_statement) /php.net/manual/en/mysqli.quickstart.prepared-statements.php)。即使[转义字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –