我基本上试图创建月末日期=今天日期,但在这种情况下22/09/2016其中opmanager存储在用户中获取该电子邮件和foreach opmanager,其具有月末日期为22/09/2016发送电子邮件。我已经完成的电子邮件脚本已准备好实现到echo
中然而,如果db获取两个不同的opmanagers,只有一个管理员收到电子邮件,而且没有被发送给foreach opmanager。
以下代码:
<?php
include ("../dbconnect.php");
$sql='SELECT * FROM `clients` WHERE endofmonthform="22/09/2016"'; //TODAYS DATE BACK HERE!
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$enddate = $row['endofmonthform']; // End
$startdate = $row['startofmonthform']; // Start
$email = $row['email']; //Email to send email to
$id = $row['id'];
$formlevel = $row['formlevel']; //To update and check formlevel
$sitegroupname = $row['mastersite'];
$manager = $row['opmanager'];
}
$query="SELECT userEmail FROM `users` WHERE userName='".$manager."'";
$resultSet = mysql_fetch_all($query);
foreach ($query as $row) {
echo $row['userEmail']; //Where send email function will be
}
?>
我需要它向每位经理发送电子邮件
答案 0 :(得分:1)
除了编码错误之外,它仅将电子邮件发送给单个管理器的原因是因为管理器位只执行一次。您的第二个问题:
SELECT userEmail FROM `users` WHERE userName='".$manager."'
将返回一组用户,其中用户名等于上一循环中的LAST $ manager,因为这段代码不在第一个循环中。
只需在循环中移动这段代码即可解决您的初始问题。其余的修复程序可以在下面找到:
include ("../dbconnect.php");
$sql='SELECT * FROM `clients` WHERE endofmonthform="22/09/2016"'; //TODAYS DATE BACK HERE!
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$enddate = $row['endofmonthform']; // End
$startdate = $row['startofmonthform']; // Start
$email = $row['email']; //Email to send email to
$id = $row['id'];
$formlevel = $row['formlevel']; //To update and check formlevel
$sitegroupname = $row['mastersite'];
$manager = $row['opmanager'];
$query="SELECT userEmail FROM `users` WHERE userName='".$manager."'";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
echo $row['userEmail']; //Where send email function will be
}
}
也就是说,这仍然是错误的代码......它使用了一个不推荐使用的API(mysql_),并且它很容易受到SQL注入攻击。请查看mysqli或pdo以获取更好的API,并prepared statements查看SQL注入问题。
如果你想用php轻松使用数据库,可以查看我的pdoWrapper:https://github.com/Mastermindzh/pdoWrapper
编辑:
正如评论中指出的那样,人们确实应该使用联接来获取这些数据。对于TS / OP来说,这可能有点多了解,但为了完整起见,我会添加它:
include ("../dbconnect.php");
$sql='SELECT c.endofmonthform, c.startofmonthform, c.email, c.id, c.formlevel, c.mastersite, c.opmanager, u.userEmail FROM `clients` as c LEFT JOIN `users` as u on c.opmanager = u.userName WHERE endofmonthform="22/09/2016"'; //TODAYS DATE BACK HERE!
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$enddate = $row['endofmonthform']; // End
$startdate = $row['startofmonthform']; // Start
$email = $row['email']; //Email to send email to
$id = $row['id'];
$formlevel = $row['formlevel']; //To update and check formlevel
$sitegroupname = $row['mastersite'];
$manager = $row['opmanager'];
echo $row['userEmail']; //Where send email function will be
}