douqiao1413 2012-07-25 01:57
浏览 48
已采纳

PHP拆分和mongodb

ok, this might sound strange, but i have a form and our business wants to track what is getting changed, when a user adds a new lead etc.

So i set up a function that does the following

function savedata($data){
    $collection = $this->db->retail_logs;
    $this->data = explode('|', $data['data']);
    print_r($this->data);

    try {
    $collection->update(
        array($this->data['0']=>$this->data['1'],$this->data[2]=>$this->data[3]),
        array("date"=> date("d.m.Y"), "time"=>date("H:i:s"),"whochanged"=>$_COOKIE['CRMUIDkey']), // new lead document to insert
        array("upsert" => true, "safe" => true)
        );
    } catch (Exception $e) {
        // Something went wrong .. 
    }
}

it is basiclly a log file.

but as you may be able to see the $data sends data though like tradingname|ABC|owner|ownerID

But if I want to add to that i would need to run a loop or a foreach I am wondering what is the best way to make sure all teh data gets saved and not just data[0] to 3 so say they send 16 fields and values in it I need a foreach or something to split it.

  • 写回答

2条回答 默认 最新

  • dtbc37573 2012-07-28 22:44
    关注

    It appears that you want to map the $data['data'] into key=>value pairs (an associative array). You want to be very careful about what fields you allow in this format especially since it looks like user-provided data (or data they can modify in a post request?). For example, a malicious user could update or add to another user's account if you aren't checking the allowed keys before doing the upsert.

    To convert the $data string you want to do something like:

    <?php
        // Keys that can be updated
        $allowed = array('tradingname','owner');
    
        // Sample data    
        $data = 'tradingname|ABC|owner|ownerID|badkey|foo';
    
        // Split into arrays based on '|' delimiter
        preg_match_all("/([^\|]+)\|([^\|]+)/", $data, $keypairs);
    
        // Combine matches into key => value array
        $keypairs = array_combine($keypairs[1], $keypairs[2]);
    
        // Sanity check to create $criteria    
        foreach ($keypairs as $key => $value) {
            if (in_array($key, $allowed)) {
                // Perhaps do some extra sanity checking that $value is expected format
                $criteria[$key] = $value;
            } else {
                // Log and/or bailout as appropriate
                echo "Ignoring: [$key] => [$value]
    ";
            }
        }
    
        // Criteria should now be reasonable to use in $collection->update(..) upsert
        print_r($criteria);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程