我只是在学习并且正在尝试将邮件表单从sql更改为PDO。 提交邮件时,将返回“页面未请求的常规错误”网页。 任何人都可以看到我做错了什么并帮助我排序吗? 这是代码,表单首先成功提交到数据库。
<?php
//mail and inert section start here====================================
if (isset($_POST['submit'])) {
// These set initial variables.
//$sendto is echoed checkbox, name="sendto[]"
$sendto = $_POST['sendto'];
$username = $_POST['username'];
$return_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//$errorurl = "../pages/Errorurl_contact.php";
if($sendto=='') {
echo"<div id ='message'><p style='padding: .5em; border: 2px solid red;'>You must pick a member to email, please click on the back button and check one of the member boxes <a href='../admin/users.php'><input type='button' value = 'Back'></a></p></div>";
}
// set variables before setting them programatically.
if($username==''){
echo"<div id ='message'><p style='padding: .5em; border: 2px solid red;'>Username is required, please click on the back button and fill in your username <a href='../admin/users.php'><input type='button' value = 'Back'></a></p></div>";
}
if (strlen($_POST['username']) >12) {
echo"<div id ='message'><p style='padding: .5em; border: 2px solid red;'>Username has to be less than 12 characters, please click on the back button and try again <a href='../admin/users.php'><input type='button' value = 'Back'></a></p></div>";
}
if (!filter_var($return_email, FILTER_VALIDATE_EMAIL))
{
echo "<div id ='message'><p style='padding: .5em; border: 2px solid red;'>Invalid email address, please click the back button and re enter your email <a href='../admin/users.php'><input type='button' value = 'Back'></a></p></div>";
}
if($subject==''){
echo "<div id ='message'><p style='padding: .5em; border: 2px solid red;'>A subject is required, please click the back button and fill in a subject for your message <a href='../admin/users.php'><input type='button' value = 'Back'></a></p></div>";
}
if (isset($subject)) {
$subject = trim($subject);
$subject = strip_tags($subject);
}
if (strlen($_POST['subject']) >60) {
echo"<div id = 'message'><p style='padding: .5em; border: 2px solid red;'>Subject has to be less than 60 characters <a href='../admin/users.php'><input type='button' value = 'Back'></a></P></div>";
}
else {
if (isset($message)) {
$message = trim($message);
$message = strip_tags($message);
}
//Check appropriate checkboxes are echoeing correct value====ERROR CHECKING===
//if(isset($_POST['sendto']))
// {
// foreach ($_POST['sendto'] as $value)
// {
// echo "sendto #{$value} was selected!\n";
// }
//}
//insert to database section==============================================
$mode="mysqli";
if($mode == "mysqli") {
if(isset($_POST['sendto']) && is_array($_POST["sendto"])){
$sendto = join('; ', $sendto);
// sql query for INSERT form submission INTO
try {
/* Prepared statement, bind and execute */
$stmt = $db->prepare("INSERT INTO mail (mailto,username,return_email,subject,message) VALUES (:sendto, :username,:return_email,:subject,:message)");
//bind parameters
$stmt->bindValue(':sendto', $sendto, PDO::PARAM_STR);
$stmt->bindParam(':username', $_POST['username'],PDO::PARAM_STR, 12);
$stmt->bindParam(':return_email', $_POST['email']);
$stmt->bindParam(':subject', $_POST['subject'], PDO::PARAM_STR, 60);
$stmt->bindParam(':message', $_POST['message'], PDO::PARAM_STR);
//protect against attacks
$username = htmlentities($username, ENT_QUOTES);
$return_email = htmlentities($return_email, ENT_QUOTES);
$subject = htmlentities($subject, ENT_QUOTES);
$message = htmlentities($message, ENT_QUOTES);
$stmt->execute();
}
catch (PDOException $e)
{
echo $e->getMessage();
}
echo "<p>Data submitted successfully</p>";
}
}
$mode = 'email';
if ($mode == "email"){
if(isset($_POST['sendto'])) {
//email variables
// email send to recipients
$email_to = "xxx@example.com";
$forward_to = implode(", ", $_POST['sendto']);
$to = "$email_to; $forward_to";// required
$email = $_POST['email']; // required
$username = $_POST['username']; // required
$subject = $_POST['subject']; // required
$message = $_POST['message'];
$from = $_POST['email'];
$headers = "From:" . $from;
mail($to, $email, $username, $subject, $message, $headers);
echo "Mail Sent.";
//testing variables present
#var_dump($username);
#var_dump($subject);
#var_dump($_POST['email']);
#var_dump($to);
#var_dump($email_message);
#var_dump($email_from);
//all good
}
}
}
}