我在数据库中有一些记录,如果特定 ID 的计数超过 4,那么我想向该特定现场电话发送 SMS。但是,如果第一次尝试发送成功,我不想再次向该特定号码发送任何后续消息。
我尝试过使用:
if( file_exist($url) ) {
die( 'already sent' );
}
file_put_content( $url, 1 );
// A lot of code
unlink( $url );
但是它不起作用。
$reminderQuery = "SELECT count(*) FROM acceptance WHERE clientID = '$clientID' ";
$reminderresult = $pdo->prepare($reminderQuery);
$reminderresult->execute();
$number_of_rows = $reminderresult->fetchColumn();
if($number_of_rows > 4){
$remindersms= "SELECT * FROM clients WHERE id = '$clientID' ";
$remindersendsms = $pdo->prepare($remindersms);
$remindersendsms->execute();
while ($remindersmsnoti = $remindersendsms->fetch()){
$phoneNos = $remindersmsnoti['phone'];
$key = "*******************"; // Remember to put your own API Key here
$phoneNo = $phoneNos;
$message = "*******************";
$sender_id = "YENKOR"; //11 Characters maximum
//encode the message
$msg = urlencode($message);
//prepare your url
$url = "*********************";
$response = file_get_contents($url) ;
}
}
我希望上面的代码在发送短信后不再发送任何消息。
您能告诉我正确的方法吗?
最佳答案
为什么不在客户表中添加一列 sms_sent
并初始设置为 0(不过,对于那些已经在数据库上拥有 4 条或更多记录的客户,您可能希望将其设置为 1)假设已经为他们发送短信的接受表)。然后将初始 SQL 更改为:
select * from clients c join (
select clientId from acceptance having count(*) >= 4
) a on c.id = a.clientId and not c.sms_sent
这将选择 acceptance
表中具有 4 条或更多记录且尚未发送 SMS 消息的所有客户(如果您只需要像示例中那样的特定客户,您可以添加额外的健康)状况)。然后,当您成功发送短信时,您可以将 clients
表上的客户端的 sms_sent
列更新为 1。
顺便说一句,由于您已经在使用准备好的语句,因此您想要传递的任何参数都应该这样做,以避免 SQL 注入(inject)攻击:
$reminderQuery = "SELECT count(*) FROM acceptance WHERE clientID = ?";
$reminderresult = $pdo->prepare($reminderQuery);
$reminderresult->execute(array($clientId());
上面假设 $clientId 是一个字符串。如果没有,那么您必须使用 bindValue
( See PDOStatement::bindValue)
关于php - 在 PHP 中,如果第一次尝试成功,如何防止第二次发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57928126/