我正在尝试制作一个小联系表单,我可以在其中向数据库中的多个用户发送电子邮件,我只是选择所有电子邮件,然后使用while循环发送到每个,但它是只发送到我的数据库中的第一封电子邮件,而不是其余的..我无法弄清楚我哪里出错了
if(isset($_POST['submit'])) {
$body= $_POST['body'];
$subject= $_POST['subject'];
$user = $_SESSION["name"];
$sql = "SELECT * FROM $user";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$email = $row["contact_email"];
mail($email, $subject, $body,'From: myemail@gmail.com');
}
}
答案 0 :(得分:1)
假设您有以下表格:
+---------+-----------------+
| user_id | email |
+---------+-----------------+
| 1 | boss1@boss.com |
| 1 | boss2@boss.com |
| 1 | boss3@boss.com |
| 2 | boris1@boss.com |
| 2 | boris2@boss.com |
+---------+-----------------+
create table emails(
user_id int references users(id),
email varchar(255) not null
);
if(isset($_POST['submit'])) {
$body = $_POST['body'];
$subject = $_POST['subject'];
$user = mysqli::real_escape_string($_SESSION["name"]);
$result = $conn->query("SELECT id from users where name='".$user."'");
$row = mysqli_fetch_array($result);
$user_id = intval($row["id"]);
$sql = "SELECT email FROM emails where user_id=".$user_id;
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$email = $row["email"];
mail($email, $subject, $body,'From: myemail@gmail.com');
}
}
请注意 user_id 对用户表的引用。
现在用PHP
{{1}}