【问题标题】:How to explode incoming sms and insert them into database in php如何在php中爆炸传入的短信并将它们插入数据库
【发布时间】:2018-08-26 12:01:03
【问题描述】:

我收到了这样格式的文本:

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

我已经尽力了。上网查了一下,还是没搞明白。

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

【问题讨论】:

  • 嗯,错误是什么?你也可以var_dump($details)看看里面有什么吗?
  • @BagusTesa,它正在插入空值。
  • 是否启用了错误报告,查询中的mysqli_error($db) 显示了什么?您还对一些可能起作用的查询使用了相同的变量。
  • @FunkFortyNiner,由于是插入日期和电话号码,所以不会返回任何错误。

标签: php mysql function explode


【解决方案1】:

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

【讨论】:

  • 谢谢。我试过了,但仍然插入空值。
猜你喜欢
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 2019-08-28
相关资源
最近更新 更多