执行代码时 PHP 脚本两次 INSERT INTO 数据库
我正在使用一个非常简单的 PHP 插入脚本,并且每次执行时都会将条目加倍。
(为了确保我没有执行该脚本两次,我使用 mail() 函数在执行时向我发送电子邮件,但我只收到一封电子邮件)
$hostname = "localhost";
$pass = "password";
$muser = "username";
$dbconn = mysql_connect($hostname, $muser, $pass);
$db ="database";
mysql_select_db($db);
$query = "INSERT INTO tablename (field1, field2) VALUES ('happy', 'birthday')";
mysql_query($query);
I'm using a very simple PHP insert into script and it doubles the entries every time it's executed.
(To be sure I'm not executing the script twice I used the mail() function to email me when it is executed and I only get one email)
$hostname = "localhost";
$pass = "password";
$muser = "username";
$dbconn = mysql_connect($hostname, $muser, $pass);
$db ="database";
mysql_select_db($db);
$query = "INSERT INTO tablename (field1, field2) VALUES ('happy', 'birthday')";
mysql_query($query);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至少有三种可能发生这种情况:
既然您说您只收到一封电子邮件,我们可能可以排除#2(除非您的电子邮件服务器有速率限制器,并且只发送两封邮件之一)。
我们可以出于同样的原因排除 #1,除非 mail() 调用不包含在与 mysql_query() 调用相同的函数中。
因此,我们只能寻找另一个复制数据的进程。您发布的代码不足以做出此决定。
There are at least three ways this could happen:
Since you said you get only one email, we can probably rule out #2 (unless your email server has a rate limiter, and only sends one of the two messages).
We can rule out #1 for the same reason, unless the mail() call is not contained in the same function as the mysql_query() call.
So we are left with looking for another process that is duplicating the data. The code you posted is not sufficient to make this determination.
这可能不太可能,但如果您从浏览器调用脚本,并且在脚本生成的页面上,您将有一个带有空
src
属性的标记,然后浏览器将假定该图像的 url 与您的脚本的 url 相同,并将额外调用您的脚本。
我认为加载外部资源的任何其他 html 标签也是如此。
事实上,你只收到一封电子邮件,这与这种情况不符,但如果我是你,我还是会检查一下。
This maybe a longshot, but if you are calling the script from the browser and on the page generated by the script you have a
<img>
tag with emptysrc
attribute then the browser will assume for this image same url as url of your script and will call your script additional time.I think same goes for any other html tag that loads external resource.
The fact that you get only one email speaks against such scenario but I'd checked it out anyway if I were you.