如何在php中分解传入的短信并将其插入数据库

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

我收到的文本格式如下:

POS;OS0101001;123456;ATAKUMOSA EAST;IWARA;TOWN HALL IWARA;755;650;50;10;10;10;15;245;5;Peacefully conducted ;2018-08-26 07:05:40

我想把文本分解成这样:

$officer_id = $details[1];
    $userpassword = $details[2];
    $lga = $db->real_escape_string($details[3]);
    $ward = $db->real_escape_string($details[4]);
    $poll_unit = $db->real_escape_string($details[5]);
    $incident_type = $db->real_escape_string($details[6]);
    $incident_priority = $db->real_escape_string($details[7]);
    $incident = $db->real_escape_string($details[8]);
    $device_entry_date = $details[9];

我确实尝试过分解它,但是在分解之后,我尝试过将它插入数据库,但是它插入了空值。
到目前为止我尝试的是:

$dir   = strtolower(trim(preg_replace('/\s+/', ' ', substr(trim($_REQUEST['Body']),0,3))));
    $msg   = substr(trim($_REQUEST['Body']),0,3);
    if($dir == 'inc'){
                    onIncidentReport($phone,$date,$msg);
        }

要插入数据库,我有:

function onIncidentReport($phone,$date,$msg){
   global $db;

       $split  =  explode(";", $msg);
       $details =  strtoupper($split[count($split)-1]);

        $officer_id = $details[1];
        $userpassword = $details[2];
        $lga = $db->real_escape_string($details[3]);
        $ward = $db->real_escape_string($details[4]);
        $poll_unit = $db->real_escape_string($details[5]);
        $incident_type = $db->real_escape_string($details[6]);
        $incident_priority = $db->real_escape_string($details[7]);
        $incident = $db->real_escape_string($details[8]);
        $device_entry_date = $details[9];

       $sql  = "SELECT * FROM master where (phoneNo = '$phone') and regstatus = 1";
       $st   =  mysqli_query($db,$sql);
       $num  =  mysqli_num_rows($st);
       $PtaW =  mysqli_fetch_assoc($st);

        if($num==1){

          $sql   = "UPDATE master SET incmsg = '$incident', incstatus = '$incident_priority', incdate = '$date', 
                    incpriotise = '$incident_priority' where (phoneNo = '$phone' OR phonetwo = '$phone') and regstatus = 1";
          $st    =  mysqli_query($db,$sql);

          $sql   = "INSERT INTO incident 
                   (lgName,phoneNo,msg,status,date) 
                     VALUES ('$lga','$phone','$incident','$incident_priority','$date')";

          $Query = mysqli_query($db,$sql);

          if($st || $Query){
            ?>
             <Response>
               <Message>
                 SUCCESSFULLY SENT.
               </Message>
             </Response>
            <?php
          }
        }else {
          ?>
             <Response>
               <Message>
                 YOU ARE NOT AUTHORISE TO USE THIS SERVICE.
               </Message>
             </Response>
          <?php
        }
   }

我已经尽力了。在网上做了一些研究,但还是搞不懂。
每次我尝试插入时,都会将空值插入到数据库中。

wf82jlnq

wf82jlnq1#

strtoupper()返回 String 对象,而不是数组。 $split 是应该用于db变量的字符串数组。 $details 只有一个字符串对象从 strtoupper() .
你需要迭代 $split 将其改为大写:

foreach($split as &$value)
    $value = strtoupper($value);

然后使用 $split db变量中的值:

$officer_id = $split[1];
    $userpassword = $split[2];
    $lga = $db->real_escape_string($split[3]);
    $ward = $db->real_escape_string($split[4]);
    //...etc

相关问题