我正在测试一个要求用户发送电子邮件的表单,然后向他发送他的姓名,手机号码。现在一切正常。它根据输入的电子邮件选择用户。它发送电子邮件。
我遇到的唯一问题是如何将表格中的数据插入发送给他的电子邮件中?
Eg.$mail->Body = "Your company details are: " Name,Surname,Cellphone;
这就是我目前面临的问题。我的完整代码如下。
我仍然很擅长PHP,所以如果这是一般性/常见错误,那么我道歉。
<?php
error_reporting(1);
ini_set('error_reporting', E_ALL);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="****"; // Mysql password
$db_name="Username"; // Database name
$tbl_name="Name"; // Table name
// Connect to server and select databse.
$conn = mysqli_connect($host, $username, $password, $db_name);
// Define $username and $password
$username=$_POST['user_name'];
$sql="SELECT * FROM $tbl_name WHERE Name='$username'";
$result=mysqli_query($conn, $sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
if ($count > 0)
{
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "****"; // SMTP server // enables SMTP debug information
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = false;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "****"; // sets the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "****"; // SMTP account username
$mail->Password = "****"; // SMTP account password
$mail->From = "Test";
$mail->FromName = "Test";
$mail->AddAddress($username, "");
$mail->isHTML(true);
$mail->Subject = 'Out Of Office Password';
$mail->Body = "Your Out Of Office password: ";
if(!$mail->Send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit();
}
else
{
echo 'Email Sent Successfully!';
}
}
&GT;
表格:
<center>
<html>
<head>
<title>User Information</title>
</head>
<body>
<form action="check-user.php" method="POST">
<h3>User Information</h3>
Email: <input type="text" name="user_name"><br>
<input type="submit" name="submit" value="Send Info">
</form>
</body>
</html>
</center>
答案 0 :(得分:1)
首先使您的连接有效
applicationHost.config
你不需要这个
$conn = mysqli_connect($host, $username, $password, $db_name)
// if u get connection valid message delete this if statement this is just for testing your connection
if ($conn) {
echo 'connection valid';
} else {
echo 'connection invalid ' . mysqli_error($conn);
}
更改此行
echo "Connected to MySQL<br />";
mysqli_select_db("$db_name") or die(mysqli_error());
echo "Connected to Database<br />";
这一个
$result=mysqli_query($conn, $sql);
因为你是php新手,我想你至少需要学习如何连接数据库,检查error_reporting,获取/更新/插入/删除数据到数据库。
我的意见是,如果您开始学习使用 if ($count > 0) {
,因为它很安全。这个PDO with prepared statements
没有逃脱会让您的安全性受到影响。
修改强>
mysqli