尝试运行脚本以通过电子邮件从我的数据库向用户发送其个人日程表。
代码应该像这样...从表1中选择所有用户,这些用户希望他们的每周电子邮件。
然后使用这些user_ids并运行查询以从表2中获取每个用户的每周计划。
如果表2中的日期距离运行代码的日期小于或等于1周,并且不等于今天的日期,则设置变量$ email_content。
如果表2中的日期超过1周,那么代码应该知道它已经完成,并且这里想继续进行,并mail()为该用户收集的信息,并仅将该信息发送给该用户,然后继续寻找下一个用户。
回声“中断”的地方实质上是标记每个用户的日程表之间的间隔,但回声约为20次。理想情况下,这部分将成为我放置mail()函数的位置,因此我不能让它也执行20次mail()函数。
我已经尝试了此代码的许多不同变体,但似乎无法正确完成。总而言之,我需要获取user1的info1,然后将info1发送给user1,然后继续获取user2的info2,然后将info2发送给user2,以此类推...
<?php
// GET USERS WHO WANT THEIR WEEKLY SCHEDULE EMAILED TO THEM (Table 1)
$sql = "SELECT * FROM XXXXXXX WHERE weekly_email = 'Yes'";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_array($result)) {
$id .= "'" . $row['user_id'] . "', ";
}
$id = trim($id, ', ');
// GET THOSE USERS' SCHEDULES (Table 2)
$sql_email = "SELECT * FROM XXXXXXXX WHERE user_id IN ($id) ORDER BY user_id, date, start_time ASC";
$result_email = mysqli_query($connection, $sql_email);
while ($row_email = mysqli_fetch_array($result_email)) {
$truck_name_email = $row_email['truck_name'];
$location_name_email = $row_email['location_name'];
$date_email = $row_email['date'];
$address_email = $row_email['address'];
$x = strtotime("7 days");
$start_email = $row_email['start_time'];
$end_email = $row_email['end_time'];
if ($date_email <= date("m/d/Y l", $x)) {
if ($date_email !== date("m/d/Y l")) {
if (!empty($address_email)) {
$address_email2 = explode(",", $row_email['address'], 2);
$email_content = $date_email . substr($date_email, 11) . " - "
. $location_name_email . ", " . $address_email2[0] . ", "
. $start_email . "-" . $end_email . "<br/>";
echo $email_content;
} elseif (empty($address_email)) {
$email_content .= $date_email . " - " . $location_name_email
. ", " . $start_email . "-" . $end_email . "<br/>";
echo $email_content;
}
}
}
if ($date_email >= date("m/d/Y l", $x)) {
echo "break";
// break;
}
}
最佳答案
只是说您应该注意潜在的SQL注入,请在用户可控制的字符串进入SQL语句之前对其进行转义。例如,如果$row['user_id']
是字符串,则必须执行mysqli_escape_string
似乎您忘记了代码中的某些内容:
$x = strtotime("7 days");
$start_email = $row_email['start_time'];
$end_email = $row_email['end_time'];
if ($date_email <= date("m/d/Y l", $x)) {
if ($date_email !== date("m/d/Y l")) { // I think that you have a $x here?
if (!empty($address_email)) {
$address_email2 = explode(",", $row_email['address'], 2);
$email_content = $date_email . substr($date_email, 11) . " - "
. $location_name_email . ", " . $address_email2[0] . ", "
. $start_email . "-" . $end_email . "<br/>";
关于php - PHP While循环在While循环中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38553347/