简体   繁体   English

选择条件SQL语句

[英]Select Conditional SQL Statement

I am posting data to a URL to send a Thank You Message. 我正在将数据发布到URL以发送感谢消息。 The details am picking are from a table called readtextfilejson . 挑选的细节来自一个名为readtextfilejson的表。 From where I am sending Thank you messages to users. 我从那里向用户发送“谢谢”消息。

I am however not able to select details of the only users who haven't received their text message. 但是,我无法选择仅未收到短信的用户的详细信息。 My code is selecting all the data and posting to all the users again and again in a loop. 我的代码是选择所有数据,然后一次又一次地循环发布到所有用户。 Thus sending multiple ThankYouMessages to users. 因此,向用户发送多个ThankYouMessages。

I have added a new column called ThankyouMessage that is by default = 'not sent'. 我添加了一个名为ThankyouMessage的新列,该列默认为=“未发送”。 So that when my script runs, it updates the ThankYouMessage column to = 'SENT', so that my script can Only select details of users who haven't yet received their thankyoumessage. 这样,当我的脚本运行时,它将ThankYouMessage列更新为='SENT',因此我的脚本只能选择尚未收到Thankyoumessage的用户的详细信息。

Thus i don't keep re-sending the same message again and again. 因此,我不会一直重复发送相同的消息。 Kindly take a look at my script below and assist how i might resolve this. 请在下面查看我的脚本,并协助我解决此问题。

My table structure: 我的表结构:

在此处输入图片说明

 <?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';
?>

2 possible issues. 2个可能的问题。

1) You are selecting the transaction regardless of what thankyoumessage is set to. 1)无论thankyoumessage设置为什么,您都在选择交易。 You may need to add a condition to that first SQL's where clause 您可能需要在第一个SQL的where子句中添加条件

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

2) When you update the transaction thankyoumessage to "SENT" you are setting all transactions, because your update statement is missing the transaction id. 2)当您将事务Thankyoumessage更新为“ SENT”时,您正在设置所有事务,因为您的更新语句缺少事务ID。 You may need to add it. 您可能需要添加它。

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

And since you want to set it to SENT regardless of what it was before, you may not need the thankyoumessage check either. 而且,由于您希望将其设置为SENT,而不管它以前是什么,所以您可能也不需要thankyoumessage检查。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM