联系表格未在数据库中注册?

我想将数据以及邮件中的某些地址发送到数据库,但数据库没有注册。


<?php


$connection = mysqli_connect('localhost', 'root', '');


if (!$connection){


die("Database Connection Failed" . mysqli_error($connection));

}

$select_db = mysqli_select_db($connection, 'cc');


if (!$select_db){


die("Database Selection Failed" . mysqli_error($connection));


}

?>




<?php


error_reporting(E_ALL);


ini_set("display_errors", 1);


?>


<?php


require_once('connect.php');


require_once('config.php');


require('PHPMailer/PHPMailerAutoload.php');


if(isset($_POST) & !empty($_POST)){


$name = $_POST['name'];


$email = $_POST['email'];


$subject = $_POST['subject'];


$message = $_POST['message'];


if(!isset($name) || empty($name)){


$error[] = "Name is required";

}


if(!isset($email) || empty($email)){


$error[] = "E-Mail is required";

}


if(!isset($subject) || empty($subject)){


$error[] = "Subject is required";

}


if(!isset($message) || empty($message)){


$error[] = "Message is required";

}


if(!isset($error) || empty($error)){


$to = "vivek@codingcyber.com";


$headers = "From : " . $email;


$mail = new PHPMailer();


$mail->isSMTP();


$mail->SMTPSecure = 'tls';


$mail->Host = $smtphost;


$mail->SMTPAuth = true;


$mail->Username = $smtpuser;


$mail->Password = $smtppass;


$mail->SMTPSecure = 'ssl';


$mail->Port = 465;


$mail->CharSet = 'UTF-8';


$mail->setFrom('', 'PixelW3 Technologies');


$mail->addAddress('', ''); 


$mail->Subject = $subject;


$mail->Body    = $message . " Name : " . $name . ". E-Mail : " . $email;


if(!$mail->send()) {


    echo 'Message could not be sent.';


    echo 'Mailer Error: ' . $mail->ErrorInfo;


} else {


    $sql = "INSERT INTO `contact` (name, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')";


    if(mysqli_query($connection, $sql)){


        echo 'Message has been sent, we will get back to you soon';


    }

}


慕尼黑的夜晚无繁华
浏览 142回答 1
1回答

慕田峪4524236

正如我上面的评论,我会将其更新为:<?php$servername = "localhost";$username = "root";$password = "<?>";$dbname = "<?>";?><?phperror_reporting(E_ALL);ini_set("display_errors", 1);?><?phprequire_once('connect.php');require_once('config.php');require('PHPMailer/PHPMailerAutoload.php');if(isset($_POST) & !empty($_POST)){$name = $_POST['name'];$email = $_POST['email'];$subject = $_POST['subject'];$message = $_POST['message'];if(!isset($name) || empty($name)){$error[] = "Name is required";}if(!isset($email) || empty($email)){$error[] = "E-Mail is required";}if(!isset($subject) || empty($subject)){$error[] = "Subject is required";}if(!isset($message) || empty($message)){$error[] = "Message is required";}if(!isset($error) || empty($error)){$to = "vivek@codingcyber.com";$headers = "From : " . $email;$mail = new PHPMailer();$mail->isSMTP();$mail->SMTPSecure = 'tls';$mail->Host = $smtphost;$mail->SMTPAuth = true;$mail->Username = $smtpuser;$mail->Password = $smtppass;$mail->SMTPSecure = 'ssl';$mail->Port = 465;$mail->CharSet = 'UTF-8';$mail->setFrom('', 'PixelW3 Technologies');$mail->addAddress('', '');&nbsp;$mail->Subject = $subject;$mail->Body&nbsp; &nbsp; = $message . " Name : " . $name . ". E-Mail : " . $email;if(!$mail->send()) {&nbsp; &nbsp; echo 'Message could not be sent.';&nbsp; &nbsp; echo 'Mailer Error: ' . $mail->ErrorInfo;} else {try {&nbsp; &nbsp; $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);&nbsp; &nbsp; // set the PDO error mode to exception&nbsp; &nbsp; $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);&nbsp; &nbsp; $stmt = $conn->prepare("INSERT INTO `contact` (name, email, subject, message) VALUES (:name, :email, :subject, :message)");&nbsp; &nbsp; $stmt->bindParam(':name', $name);&nbsp; &nbsp; $stmt->bindParam(':email', $email);&nbsp; &nbsp; $stmt->bindParam(':subject', $subject);&nbsp; &nbsp; $stmt->bindParam(':message', $message);&nbsp; &nbsp; $stmt->execute();}catch(PDOException $e){echo "Error: " . $e->getMessage();}$conn = null;/*if(mail($to, $subject, $message, $headers)){&nbsp; &nbsp; $sql = "INSERT INTO `contact` (name, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')";&nbsp; &nbsp; if(mysqli_query($connection, $sql)){&nbsp; &nbsp; &nbsp; &nbsp; "Message Received, we will get back to you soon";&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; echo "Failed to send message, Try again";&nbsp; &nbsp; }}else{&nbsp; &nbsp; echo "Failed to send message, Try again";}*/}}?><!DOCTYPE html><html><head><title>Simple Contact form in PHP & MySQL</title><!-- Latest compiled and minified CSS --><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" ></head><body><div class="container"><div class="row">&nbsp; &nbsp; <form class="col-md-6 col-md-offset-3" method="post">&nbsp; &nbsp; &nbsp; &nbsp; <h2>Contact Us</h2>&nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <label for="exampleInputEmail1">Name</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Full Name" required="">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <label for="exampleInputEmail1">Email address</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email" required="">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <label for="exampleInputPassword1">Subject</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="subject" class="form-control" id="exampleInputPassword1" placeholder="Enter Your Subject" required="">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <textare`enter code here`a class="form-control"&nbsp; name="message" rows="3" placeholder="Enter Your Query Here" required=""></textarea>&nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn`enter code here`-default">Submit</button>&nbsp; &nbsp; </form></div></div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP