我正在处理一个简单的表单,只有2个字段。姓名和电子邮件。我到目前为止所取得的成就是。
我遇到问题的地方是我需要将数据与数据库中的第三个字段一起发送给提交者,这是自动增量主键作为其成员编号。我的问题是如何根据数据库条目发送电子邮件。
即;感谢您加入会员ID的“提交者姓名”是“主要密钥”。 这是我目前的代码。
的index.html
<form method="post" action="doit.php">
Name<input name="Name" type="text" /><br>
Email<input name="Email" type="text" /><br>
<input name="Submit" type="submit" value="submit" />
</form>
doit.php
<?php
$con = mysqli_connect('localhost','DB_Username','DB_Password');
if(!$con)
{
echo 'Not Connected To Server';
}
if(!mysqli_select_db($con,'DB_Name'))
{
echo 'Database Not Selected';
}
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$sql = "INSERT INTO Table_name (Name,Email) VALUES ('$Name','$Email')";
if(!mysqli_query($con,$sql))
{
echo 'Not Inserted';
}
else
{
echo 'Inserted';
}
header ("refresh:2; url=auto-response.php");
?>
自动response.php
<?php
if ($_SERVER["REQUEST_METHOD"}=="POST") {
$connnection=mysqli_connect("localhost","DB_username","DB_password","DB_Name");
// Check connection
if ($connection) {
echo "connection successfull! <br>;"
} else {
die("connection failed. Reason ". mysqli_connect_err())
}
{
$Email=$_POST["Email"];
$Name=$_POST["Name"];
$ID=$_POST["ID"];
$sql="SELECT ID,Email,Name FROM Table_Name WHERE Email='".$Email."'";
$result = mysqli_query($connection,$sql);
$to = 'Email';
$subject = 'Thank you For Joining';
$message = 'Thank you for joining "Email" Your Member ID is "ID"';
$headers = 'From: dsarmy@thedarksideshop.com' . "\r\n" .
'Reply-To: dsarmy@thedarksideshop.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
答案 0 :(得分:0)
Mysql有自己的功能,可以给你id。虽然您在插入时已经有了名称。
// Using the below line right after your insert you can get the primary id.
$id = $connection->insert_id; // Get latest inserted id.
希望它有所帮助。
编辑:根据您所做的编辑,我已将您的代码安排到应该如何运作。现在试试这个。
<强>的index.html 强>
<form method="post" action="doit.php">
Name<input name="Name" type="text" /><br>
Email<input name="Email" type="text" /><br>
<input name="Submit" type="submit" value="submit" />
</form>
<强> doit.php 强>
$con = mysqli_connect('localhost','DB_Username','DB_Password');
if(!$con)
{
echo 'Not Connected To Server';
}
if(!mysqli_select_db($con,'DB_Name'))
{
echo 'Database Not Selected';
}
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$sql = "INSERT INTO Table_name (Name,Email) VALUES ('$Name','$Email')";
if(!mysqli_query($con,$sql))
{
echo 'Not Inserted';
}
else
{
echo 'Inserted';
$id = $con->insert_id; // Get latest inserted id.
$to = $Email;
$subject = 'Thank you For Joining';
$message = 'Thank you for joining "'.$Name.'" Your Member ID is "'.$id.'"';
$headers = 'From: dsarmy@thedarksideshop.com' . "\r\n" .
'Reply-To: dsarmy@thedarksideshop.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}