为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕尼黑的夜晚无繁华 2022-01-08 19:59:41
我想将数据以及邮件中的某些地址发送到数据库,但数据库没有注册。<?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));}?><?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('', ''); $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';    }}
查看完整描述

1 回答

?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

正如我上面的评论,我会将其更新为:


<?php

$servername = "localhost";

$username = "root";

$password = "<?>";

$dbname = "<?>";

?>


<?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 {


try {

    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    // set the PDO error mode to exception

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


    $stmt = $conn->prepare("INSERT INTO `contact` (name, email, subject, message) VALUES (:name, :email, :subject, :message)");

    $stmt->bindParam(':name', $name);

    $stmt->bindParam(':email', $email);

    $stmt->bindParam(':subject', $subject);

    $stmt->bindParam(':message', $message);

    $stmt->execute();

}catch(PDOException $e)

{

echo "Error: " . $e->getMessage();

}

$conn = null;


/*

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


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


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


        "Message Received, we will get back to you soon";


    }else{

        echo "Failed to send message, Try again";


    }


}else{


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


    <form class="col-md-6 col-md-offset-3" method="post">


        <h2>Contact Us</h2>


      <div class="form-group">


        <label for="exampleInputEmail1">Name</label>


        <input type="text" name="name" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Full Name" required="">


      </div>


      <div class="form-group">


        <label for="exampleInputEmail1">Email address</label>


        <input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email" required="">


      </div>


      <div class="form-group">


        <label for="exampleInputPassword1">Subject</label>


        <input type="text" name="subject" class="form-control" id="exampleInputPassword1" placeholder="Enter Your Subject" required="">


      </div>


      <textare`enter code here`a class="form-control"  name="message" rows="3" placeholder="Enter Your Query Here" required=""></textarea>


      <button type="submit" class="btn btn`enter code here`-default">Submit</button>


    </form>


</div>

</div>


</body>


</html>



查看完整回答
反对 回复 2022-01-08
  • 1 回答
  • 0 关注
  • 141 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信