select条件sql语句

5vf7fwbs  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(355)

我发布数据到一个网址发送感谢信息。我挑选的细节来自一个名为readtextfilejson的表。从这里我向用户发送感谢信息。
但是,我无法选择的细节,只有用户谁没有收到他们的短信。我的代码是选择所有的数据并在一个循环中一次又一次地发布给所有的用户。从而向用户发送多条感谢信息。
我添加了一个名为thankyoumessage的新列,默认情况下为“not sent”。因此,当我的脚本运行时,它会将thankyoumessage列更新为=“sent”,以便我的脚本只能选择尚未收到thankyoumessage的用户的详细信息。
因此,我不会一次又一次地重复发送相同的消息。请看一下我下面的脚本,并帮助我如何解决这个问题。
我的表结构:

<?php

        $data = (string) file_get_contents($file);

        //echo $data;

        $data = str_replace('//Confirmation Respose', '', $data);
        $data = str_replace('// Validation Response', '', $data);
        $data = str_replace(' ', '', $data);
        $data = preg_replace('/\s+/S', " ", $data);
        $data = trim($data);
        $pattern = '/\s*/m';
        $replace = '';

        $testString = $data;

        $removedWhitespace = preg_replace( $pattern, $replace,$testString );
        $removedWhitespace2 = str_replace (' ', '', $testString);

        $getAllData = explode('}{', $removedWhitespace2);
        foreach ($getAllData as $row) {
           $row = str_replace('{', '', $row);

           $rowData = explode(',"', $row);
           $rowData = explode(',"', $row);
           $columnValues = array();
           $chkTransId = '';

           foreach ($rowData as $value) {
              $newVal = explode(':', $value);
              $key = str_replace('"', '', $newVal[0]);
              $val = str_replace('"', '', $newVal[1]);
              $val = trim($val);

              $columnValues[] = ($val) ? "'$val'": "''";
              if($key == 'TransID'){
                 $chkTransId = $val;
              }
           }
           if($chkTransId == ''){
              continue;
           }
           ////THIS IS THE SECTION AM HAVING PROBLEMS WITH - I WANT TO
           ////SELECT ONLY THE DATA WHERE THE COLUMN  WHERE thankyoumessage =            
            ///// 'NOT SENT'
           $chkSql = "select * from `readtextfilejson`where TransID='$chkTransId'";

           $getResult = mysqli_query($con, $chkSql); 
           $getCount = mysqli_num_rows($getResult);
           $row = mysqli_fetch_object($getResult);

$text = "Dear ". $row->FirstName ." Your Payment of ". $row->TransAmount ." to XXXXX was Received Succesfully. Confirmation Code: ". $row->TransID  ."";
   $destination = array("messageId"=>"$product_id","to"=>"$row->MSISDN");
   $product_id=uniqid();

 $notifyUrl = "URL";
    $notifyContentType = "application/json";
    $callbackData = 'eHostOnlineCodeCheck125690';
    $username = "USERNAME";
    $password = "PASSWORD";
    $postUrl = "POSTURL";

    $message = array("from" => "USERNAME",
            "destinations" => $destination,
            "text" => $text,
            "bulkId" => "",
        "notifyUrl" => $notifyUrl,
        "flash" => "false",
            "notifyContentType" => $notifyContentType,
        "callbackData" => $callbackData);       
    $postData = array("messages" => array($message));
    $postDataJson = json_encode($postData);
    //Submit all data to SMS server
    $ch = curl_init();
    $header = array("Content-Type:application/json", "Accept:application/json");
    curl_setopt($ch, CURLOPT_URL, $postUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // response of the POST request
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $responseBody = json_decode($response);

    curl_close($ch);

echo "<pre>";
print_r($row);
print_r($responseBody);
echo "</pre>";

 $Sql = "UPDATE readtextfilejson SET thankyoumessage = 'SENT' WHERE thankyoumessage = 'not sent'";
  mysqli_query($con, $Sql) or die(mysqli_error($con)); 

   if($getCount > 0){
      continue;
   }

   $columnValues = implode(',', $columnValues);
   $sql = "INSERT INTO `readtextfilejson`(`TransactionType`, `TransID`, `TransTime`, `TransAmount`, `BusinessShortCode`, `BillRefNumber`, `InvoiceNumber`, `OrgAccountBalance`, `ThirdPartyTransID`, `MSISDN`, `FirstName`, `MiddleName`, `LastName`) VALUES (".$columnValues.")";

   mysqli_query($con, $sql) or die(mysqli_error($con)); 
}
echo 'Data inserted successfully';
?>
ioekq8ef

ioekq8ef1#

2个可能的问题。
1) 无论thankyoumessage设置为什么,您都在选择事务。您可能需要向第一个sql的where子句添加一个条件

SELECT * FROM `readtextfilejson`
WHERE TransID = '$chkTransId' AND thankyoumessage = 'not sent'

2) 当您将事务thankyoumessage更新为“已发送”时,您正在设置所有事务,因为您的update语句缺少事务id。您可能需要添加它。

UPDATE readtextfilejson 
SET thankyoumessage = 'SENT' 
WHERE thankyoumessage = 'not sent' AND TransID = '$chkTransId'

而且,由于您希望将其设置为“已发送”,而不管它以前是什么,因此您可能也不需要“thankyoumessage”检查。

UPDATE readtextfilejson 
SET thankyoumessage = 'SENT' 
WHERE TransID = '$chkTransId'

相关问题