dongzhan8001 2014-09-09 15:26 采纳率: 0%
浏览 21
已采纳

foreach不是通过数组迭代

I'm working on a fairy simple saving class using the below code, whenever I run it, only the first if statement is triggered one time, the data is saved in the logs_all table once and once only. All variables passed to the function fine.

I have spent an hour debugging and have honestly no idea what the problem is, could anyone assist?

function saveLog($logtype, $user, $category, $department, $item, $action) {

            $logtype = 'all,buying';
            $a = explode(",",$logtype);

            foreach($a as $value) {

                if($value == 'all') {

                    $db = new mysqli(DB_HOST,DB_USERNAME, DB_PASSWORD, DB_NAME);
                    $sql = "INSERT INTO logs_all (user, category, department, item, action, date) VALUES ('$user', '$category', '$department', '$item', '$action', NOW())";
                    $db->query($sql);
                    return $db->insert_id;


                }

                if($value == 'buying') {

                    $db = new mysqli(DB_HOST,DB_USERNAME, DB_PASSWORD, DB_NAME);
                    $sql = "INSERT INTO logs_buying (user, category, department, item, action, date) VALUES ('$user', '$category', '$department', '$item', '$action', NOW())";
                    $db->query($sql);
                    return $db->insert_id;


                }

            }

}
  • 写回答

3条回答 默认 最新

  • dpecb06062 2014-09-09 15:28
    关注
    return $db->insert_id;
    

    This is your problem. The return statement returns out of the context of the function, or in a global case, the PHP script entirely. This means the foreach stops executing along with any other PHP code inside the function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染