简体   繁体   English

PHP / MySQL INSERT执行两次

[英]php/MySQL INSERT executing twice

I've tried various ways like object and non-object, and multiple examples around the web. 我尝试了各种方法,例如对象和非对象,以及网络上的多个示例。 I know this has been asked before, but for some reason nothing has solved my issue. 我知道之前已经有人问过这个问题,但是由于某种原因,没有任何东西可以解决我的问题。 I've done plenty of INSERTS in the past and have never ran into this problem before. 过去,我已经做了很多插入工作,以前从未遇到过这个问题。

I just want to log ip addresses and referral urls to help fight against adwords fraud/sabotage, but for some reason it's being executed twice. 我只想记录ip地址和引荐网址以帮助对抗adwords欺诈/破坏活动,但由于某种原因,它被执行了两次。 The first time, the referral is blank and the second time the referring url is itself. 第一次,引荐为空白,第二次引荐网址为自身。

Here is the current code I'm using: 这是我正在使用的当前代码:

$db = 'defensalegal';
$table = 'dogbite_tracker';

$ip = $_SERVER['REMOTE_ADDR'];
$referer = $_SERVER['HTTP_REFERER'];

$conn = new mysqli($host, $user, $pass, $db);

$conn->query('INSERT INTO '.$table.' (ID, ip, views, timestamp, referer) VALUES("", "'.$ip.'", "1", "'.date('Y-m-d').' @ '.date('H:i:s').'", "'.$referer.'")');
$conn->close();

I've verified it's not a refresh issue since it does it when you directly type in the url as well, and I also wiped the entire file of everything except the above code snippet in case there was some sort of conflict happening, still got duplicate inserts. 我已经验证这不是刷新问题,因为当您直接键入url时也会这样做,并且还擦除了上述代码段以外的所有文件,以防发生某种冲突,但仍然会重复插入。

I'm not getting any errors as either. 我也没有任何错误。

The quick fix would be 快速修复将是

CREATE UNIQUE INDEX index_ip_timestamp_referer ON dogbite_tracker(ip, timestamp, referer);

This is quick solution of your problem, run this query on your DB server (SQL Console) 这是您问题的快速解决方案,请在数据库服务器上运行此查询(SQL控制台)

For exact solution, You need to post your full script (Code). 为了获得确切的解决方案,您需要发布完整的脚本(代码)。

Looks like your script is doable to make duplicate insert, as stated previously to make unique index your script should 看起来您的脚本可以进行重复的插入,如前所述,可以为脚本创建唯一索引
1. Check if previous ip already on database 1.检查先前的ip是否已经在数据库中
2. Update the ip if exists 2.更新ip(如果存在)
3. Insert new if ip not exists 3.如果ip不存在,请插入新的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM