mysql查询多次插入数据

时间:2020-09-07 06:50:30

标签: php mysql insert sql-insert

我有一个表log_Data,我在其中记录用户输入的搜索关键字。我面临一个问题,即mysql查询多次插入数据。查询非常简单INSERT INTO TABLE_NAME (id, keyword, date_adde) ...,但是如果用户输入一些单词...查询将插入它,几秒钟后它将再次插入,有时会插入相同的记录4 5次。

我该如何限制插入并使其更加准确,从而避免重复并获得更好,更准确的搜索报告。

编辑

在我的控制器内:

        if(isset($this->request->get['search']) && $this->request->get['search'] != ' '){
            $search_log = array(
                'keyword' => $this->request->get['search'],
                'source'  => '0', //0 for website and 1 for application
                'total_result' => $product_total
            );
            $this->model_catalog_product->searchLog($search_log);
        }

在我的模型中,我有:

public function searchLog($data){
    $query = $this->db->query("INSERT INTO search_log SET keyword = '" . $this->db->escape($data['keyword']) . "', record = '" . (int)$data['total_result'] . "', customer_id = '" . (int)$this->customer->getId() . "', source = '" . (int)$data['source'] . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', date_added = NOW()");
}

附上一张图片,其中顾客搜索heel“以黑色突出显示”被插入7次,相差几秒钟...该顾客最初仅搜索1次...与其他词相同好吧...

enter image description here

2 个答案:

答案 0 :(得分:0)

首先,检查您的代码。传出秘诀密钥:

md5(关键字+日期+小时)

答案 1 :(得分:0)

  • 尝试通过添加会话保护来避免任何形式的重复,添加隐藏的输入,将其称为doupleprotection,它的值来自诸如rand(1000,10000000000)或md5(time())之类的随机化,然后将该值保存在$ _SESSION [ '双重保护']
  • 提交表单后,在插入数据库之前,将输入值与$ _SESSION ['doubleprotection']进行比较,如果相等,则从rand(1000,10000000000)中给$ _SESSION ['doubleprotection']新值
  • 如果输入值和$ _SESSION ['doubleprotection']不等于停止查询
相关问题