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

标签 php mysql function explode

我收到了以下格式的传入文本:

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];

我实际上尝试过 explode 它,但 explode 后,我尝试将其插入数据库,但它插入了空值。

到目前为止我尝试过的:

$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
        }
   }

我已经尽力了。在网上查了一些资料,还是没搞明白。

每次我尝试插入时,都会将空值插入到数据库中。

最佳答案

strtoupper()返回一个 String 对象,而不是数组。 $split 是应该用于数据库变量的字符串数组。 $details 只是从 strtoupper() 返回的单个 String 对象。

您需要迭代 $split 将其更改为大写:

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

然后在数据库变量中使用$split值:

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

关于php - 如何在php中分解传入的短信并将其插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52026041/

相关文章:

PHP/MySQL - 登录系统 - SELECT 整个数据库是否聪明?

java - JPA:可自动和手动设置的 Id 列

mysql - 有没有办法查询一个类是否包含具有某个已知名称的实例变量?

javascript - 我不明白这个功能。 [Codecademy 联系人列表练习]

javascript - 如何设置 String#replace 回调的参数?

c++ - 平均值、RMS 和过零数数组 c++

PHP SELF 表单发布到数据库

php - 如何将mysql中的列值更改为行值?

php - 将视频上传到特定的播放列表

php - 将 mysql 调用加载到数组中