执行代码时 PHP 脚本两次 INSERT INTO 数据库

发布于 2024-11-09 02:50:40 字数 394 浏览 0 评论 0原文

我正在使用一个非常简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风和你 2024-11-16 02:50:40

至少有三种可能发生这种情况:

  1. 您发布的代码包含在被调用两次的函数中。
  2. 您的页面将重新加载,执行调用两次。
  3. 另一个进程(cron 作业、备份实用程序)正在复制数据。

既然您说您只收到一封电子邮件,我们可能可以排除#2(除非您的电子邮件服务器有速率限制器,并且只发送两封邮件之一)。

我们可以出于同样的原因排除 #1,除非 mail() 调用不包含在与 mysql_query() 调用相同的函数中。

因此,我们只能寻找另一个复制数据的进程。您发布的代码不足以做出此决定。

There are at least three ways this could happen:

  1. The code you posted is contained in a function that gets called twice.
  2. Your page is reloaded, executing the call twice.
  3. Another process (cron job, backup utility) is duplicating the data.

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.

夜光 2024-11-16 02:50:40

这可能不太可能,但如果您从浏览器调用脚本,并且在脚本生成的页面上,您将有一个带有空 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 empty src 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文