我正在处理的这个脚本从数据库中检索电子邮件,如果它们符合任何条件,将发送一封电子邮件,并更新数据库,说明已发送电子邮件。
我可以让脚本执行此操作,但我无法通过迭代遍历数组中的所有电子邮件。它将运行一个电子邮件的脚本,然后它将出错:在非对象上调用成员函数execute()。所以我从中理解的是,当foreach循环遍历另一封电子邮件时,该变量内部没有任何内容可供查询插入数据库。
如果我通过刷新页面再次运行脚本,它将插入在上次调用期间没有发送的电子邮件,但它会为阵列中的下一封电子邮件提供相同的错误。
所以我的问题是:如何让我的脚本迭代遍历数组中的所有电子邮件,而不会出错?
date_default_timezone_set('America/Los_Angeles');
$today = date('m/d/Y h:i:s a');
$time = time();
require('addrow_info.php');
//connect to database
$mysqli = new mysqli('localhost', $username, $password, $database);
if(!$mysqli){
die('Not connected :' . mysqli_error($mysqli));
}
$query = $mysqli->query("SELECT * FROM abandoned WHERE orderStatus IS NULL ORDER BY time DESC");
if(!$query){
die('Invalid query :' . mysqli_error($mysqli));
}
while($email = mysqli_fetch_array($query)){
$abandoned[] = array(
'email' => $email['email'],
'time' => $email['time'],
'status' => $email['orderStatus'],
'attempts' => $email['attempts']
);
}
$mysqli->close();
//open new connection
$mysqli = new mysqli('localhost',$username,$password,$database);
if(!$mysqli){
die('Not connected : ' . mysqli_error($mysqli));
}
foreach($abandoned as $viewAbandoned){
$timeAbandoned = strtotime($viewAbandoned['time']);
if($timeAbandoned < strtotime('-1 hour') && $viewAbandoned['attempts'] == '0'){
echo "Order by " . $viewAbandoned['email'] . " was abandoned more than 1 hour ago<br/>";
//send email to remind them of items in their cart
$to = 'josan.iracheta@gmail.com';
$subject = 'new abandoned email';
$message = $viewAbandoned['email'];
$sendmail = mail($to,$subject,$message);
if(!$sendmail){
echo "Mail not sent!";
}
else{
echo "Mail sent!";
//update database with email attempt
$update = $mysqli->query("UPDATE abandoned SET attempts = '1' WHERE email = '".$viewAbandoned['email']."' ");
$updateResult = $update->execute();
}//end if Mail was sent, database was updated with attempt
}//end if abandoned cart is more than 1 hour old
//if email has been sent once, send after 6 hours
elseif($timeAbandoned < strtotime('-6 hours') && $viewAbandoned['attempts'] == '1'){
//send email to remind them of items in their cart
$to = 'josan.iracheta@gmail.com';
$subject = 'new abandoned email';
$message = 'Second notice ' . $viewAbandoned['email'];
$sendmail = mail($to,$subject,$message);
if(!$sendmail){
echo "Mail not sent!";
}
//if mail is sent
else{
echo "Mail sent!";
//update database with email attempt
$update = $mysqli->query("UPDATE abandoned SET attempts = '2' WHERE email = '".$viewAbandoned['email']."' ");
$updateResult = $update->execute();
}//end if Mail was sent
//database was updated with attempt
}
//send email after 24 hours
elseif($timeAbandoned < strtotime('-24 hours') && $viewAbandoned['attempts'] == '2'){
//send email to remind them of items in their cart
$to = 'josan.iracheta@gmail.com';
$subject = 'new abandoned email';
$message = 'third notice' . $viewAbandoned['email'];
$sendmail = mail($to,$subject,$message);
if(!$sendmail){
echo "Mail not sent!";
}
else{
echo "Mail sent!";
//update database with email attempt
$update = $mysqli->query("UPDATE abandoned SET attempts = '3' WHERE email = '".$viewAbandoned['email']."' ");
$updateResult = $update->execute();
}//end if Mail was sent
//database was updated with attempt
}
//send email after one week
elseif($timeAbandoned < strtotime('-168 hours') && $viewAbandoned['attempts'] == '3'){
//send email to remind them of items in their cart
$to = 'josan.iracheta@gmail.com';
$subject = 'new abandoned email';
$message = 'It has been one week already' . $viewAbandoned['email'];
$sendmail = mail($to,$subject,$message);
if(!$sendmail){
echo "Mail not sent!";
}
else{
echo "Mail sent!";
//update database with email attempt
$update = $mysqli->query("UPDATE abandoned SET attempts = '4' WHERE email = '".$viewAbandoned['email']."' ");
$updateResult = $update->execute();
}//end if Mail was sent
//database was updated with attempt
}
}//end foreach loop
答案 0 :(得分:1)
我认为您的计划在点击$updateResult = $update->execute();
行时会中断,因为$mysqli->query()
对于成功的 DML 查询返回true。 Documentation Here
我相信如果你用执行调用删除行,它应该解决你的问题。
如果您想保留执行行,则需要将query()
更改为prepare()