dongye9228 2018-05-06 13:25
浏览 51
已采纳

由于数组数据类型错误,无法插入mysql表

Following is the array from which the data are retrieved and tried to be inserted into mysql table but works well when more than 1 row to be inserted and fails when only 1 row to be inserted.

// The structure of the array is as follows
Array
(
    [Row] => Array
        (
            [0] => Array
                (
                    [PIN] => 1274
                    [DateTime] => 2018-04-07 09:28:16
                    [Verified] => 15
                    [Status] => 3
                    [WorkCode] => 0
                )

            [1] => Array
                (
                    [PIN] => 157
                    [DateTime] => 2018-04-07 10:22:56
                    [Verified] => 15
                    [Status] => 3
                    [WorkCode] => 0
                )
    // these are the raw punch data from biometric machine

Following is the piece of code which currently being used.

  if(isset($array_att_logs) && isset($array_att_logs['Row'])) {     
      foreach ($array_att_logs['Row'] as $value) {
          $emp_code =  $value['PIN'];       // line 92                                  $dateNTime =  $value['DateTime']; //line 93

$punch_query = "INSERT IGNORE INTO punching_data_table (emp_code, date_time,in_out_status) VALUES ('$emp_code', '$dateNTime','$in_out_status')";

 $punch_result = mysql_query($punch_query);
 echo mysql_error();
         }       
     } echo var_dump($array_att_logs);

Please note that the $in_out_status is defined explicitly elsewhere which does not seem to have any problem at all.I have checked using var_dump() that the array exists with data just fine. The problem is if there is a single row to be inserted into the table then following error is shown.

Warning: Illegal string offset 'PIN' in /home1/.../punch-raw.php on line 92
Warning: Illegal string offset 'DateTime' in /home1/..../punch-raw.php on line 93

If there is more than 1 row to be inserted into the table then it works just fine. May be I am defining $emp_code and $dateNTime incorrectly.

Here is how it looks like when there is a single row.

 echo " <pre>"; 
    print_r($array_att_logs); 
    echo "/<pre>"; 

Array
(
    [Row] => Array
        (
            [PIN] => 406
            [DateTime] => 2018-05-06 14:40:09
            [Verified] => 1
            [Status] => 3
            [WorkCode] => 0
        )

)
/

using var_dump it looks like below

array(1) { ["Row"]=> array(5) { ["PIN"]=> string(3) "406" ["DateTime"]=> string(19) "2018-05-06 14:40:09" ["Verified"]=> string(1) "1" ["Status"]=> string(1) "3" ["WorkCode"]=> string(1) "0" } }

</div>
  • 写回答

1条回答 默认 最新

  • duanlan7239 2018-05-07 07:07
    关注

    The reason that you are having the problem is that the array structure of $array_att_logs is different when there is only 1 row to when there are more than one row. Thus you need to test for that situation and change the way you iterate through the array. The easiest way to check if there is only 1 row is to see if the contents of $array_att_logs['Row'] is an array of arrays. To do this we use array_values($array_att_logs['Row']) to ensure that we have an entry with an index of 0, and then test that value to see if it is an array. Then we use that result to change the value being iterated by the foreach:

    $row = array_values($array_att_logs['Row']);
    $multirows = is_array($row[0]);
    foreach (($multirows ? $array_att_logs['Row'] : $array_att_logs) as $value) {
        $emp_code =  $value['PIN'];
        ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值