我创建了一个程序,可以在特定条件下向我的数据库中的所有电子邮件发送电子邮件。然而,我现在的问题是,我需要在连接到我的另一个php页面的电子邮件的消息中创建一个链接。在该链接中,我需要将电子邮件地址传递给查询字符串,但我从未创建变量$ email,因为我觉得我的while循环没有必要。关于如何解决这个问题的任何建议?
http://localhost/unsubscribe.php?e = $ email'>点击此处取消订阅
<?php
$subject = "New Sales";
$message = "25% off any item of your choice! <br>
<a href=’http://localhost/unsubscribe.php?e=$email'>click here to unsubscribe</a>";
$headers = "From: email@yourdot.com";
//create connection
$servername = "localhost";
$username = "root";
$password = "student";
$dbname = "db1";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$result = $mysqli->query("SELECT emailAddress FROM members WHERE subscribe = 'YES' ");
while ($row = $result->fetch_object()) {
if (isset($row->emailAddress)) {
mail($row->emailAddress,$subject, $message, $headers);
}
}
echo "Email has been sent!";
?>
任何建议都有帮助,但我不想摆脱while循环,因为在我看来这是最好的选择。如果有更好的选择,请通知我。请保持对我的轻松。我是个菜鸟。
答案 0 :(得分:0)
如果你只是想通过url传递变量,你可以像这样传递:
<a href=’http://localhost/unsubscribe.php?e=<?php echo $email; ?>'>click here to unsubscribe</a>";
或
<a href='unsubscribe.php?e=".$email."'>click here to unsubscribe</a>
答案 1 :(得分:0)
如果条件在while循环中,请移动电子邮件,如下所示:
while ($row = $result->fetch_object()) {
if (isset($row->emailAddress)) {
$message = "25% off any item of your choice! <br>
<a href=’http://localhost/unsubscribe.php?e=$row->emailAddress'>click here to unsubscribe</a>";
mail($row->emailAddress,$subject, $message, $headers);
}
}
希望它有所帮助!