Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提交时发送申请表并插入数据库_Php_Mysql_Forms_Email - Fatal编程技术网

Php 提交时发送申请表并插入数据库

Php 提交时发送申请表并插入数据库,php,mysql,forms,email,Php,Mysql,Forms,Email,我试图将提交时表单输入字段中的数据发送到电子邮件地址,同时将表单数据插入到数据库表中。我已经成功地将表单数据提交到数据库中,现在我正在努力将表单数据作为电子邮件发送。下面是我的PHP代码。所有PHP代码都在一个文件中。我试图将数据发送到的电子邮件地址不是gmail、hotmail等电子邮件,而是公司自己的邮寄地址 <?php $conn = mysqli_connect("", "", "", ""); --> deleted this because of this post

我试图将提交时表单输入字段中的数据发送到电子邮件地址,同时将表单数据插入到数据库表中。我已经成功地将表单数据提交到数据库中,现在我正在努力将表单数据作为电子邮件发送。下面是我的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);
以下仅是my 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>
要补充的是,当我尝试在一个文件中提交包含上述所有代码的应用程序时,在提交时,不会向数据库发送包含同一文件中电子邮件代码的数据。如果我删除了同一文件中的电子邮件代码,则申请表将提交到数据库。我必须尽可能简洁,希望有人能帮助我!谢谢大家!

根据这条线:

mail("example@example.com", $course, $firstname, $surname, $DOB, $gender, $address1, $address2, $city, $postcode, $mobile, $home, $email, $residency, $learning, $qualifications);
这不是邮件的工作方式

语法是:

bool邮件字符串$to,字符串$subject,字符串$message[,字符串$additional_headers[,字符串$additional_parameters]]

您对mail使用的参数太多,应该只有四个。 为所有要发送的变量分配一个变量,并将其设置为正文参数,并使用手册中的标题

即:

您的代码也可以接受SQL注入,请使用准备好的语句:

还要确保所有POST数组都是正确的并保留值

如果PHP中存在任何错误,错误报告将有所帮助:

您也可以在标题之前输出

您可以移除echo并将其替换为标头,反之亦然;你不能两者都用

如果您在发送邮件时仍然遇到问题,请咨询以下问答:


这表明电子邮件代码中存在语法错误。打开PHP错误报告,看看会出现什么。另外,您的代码容易受到SQL注入的影响。请将准备好的语句与mysqli一起使用,以便了解有关的语句。即使是这样也不安全!你检查错误日志了吗?邮件不是这样工作的,为所有要发送的变量分配一个变量,并将其设置为body参数,并使用我给你的手动链接中的标题。
mail("example@example.com", $course, $firstname, $surname, $DOB, $gender, $address1, $address2, $city, $postcode, $mobile, $home, $email, $residency, $learning, $qualifications);
$to = "example@example.com";
$subject = "Mail form submitted";

$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender

$body = "

$course \n $firstname \n $surname \n $DOB \n $gender \n 
$address1 \n $address2 \n   $city \n $postcode \n $mobile \n 
$home \n $email \n $residency \n $learning \n $qualifications

";

if(mail($to, $subject, $body, $headers)){

    echo "Mail sent";
}
else {
    echo "Error, check your logs";
    // header("Location: apply.php"); exit;
    // use echo OR header, not both and uncomment/comment out the one you want.
}