PHP API脚本两次插入MySQL数据库

标签 php mysql

从浏览器运行以下脚本后,我发现它两次将数据插入我的 MySQL 数据库。我只在我的代码中进行一次调用,一切似乎都井井有条。我在想这可能与在 if 语句中调用查询有关,但我找不到与此相关的任何内容。

我正在使用以下查询运行脚本:

http://businesstools.vmem.int/performance/api/start.php?device=7300&config=thin

当我在此表上运行 SELECT 语句时,它返回以下内容。星号放在我运行的查询旁边,未标记的行作为辅助插入项出现。

mysql> SELECT * FROM test;
+-----+-----+-----+---------------------+
| tid | cid | did | runtime             |
+-----+-----+-----+---------------------+
|  *1 |   1 |   5 | 2015-12-22 15:54:56 |
|   2 |   1 |   5 | 2015-12-22 15:55:29 |
|  *3 |   1 |   5 | 2015-12-22 15:57:52 |
|   4 |   1 |   5 | 2015-12-22 15:57:57 |
|  *5 |   0 |   5 | 2015-12-22 15:57:59 |
|   6 |   0 |   5 | 2015-12-22 16:06:28 |
|  *7 |   0 |   5 | 2015-12-22 16:06:31 |
|  *8 |   1 |   5 | 2015-12-22 16:06:35 |
|  *9 |   1 |   5 | 2015-12-22 16:06:38 |
| *10 |   1 |   5 | 2015-12-22 16:06:41 |
| *11 |   1 |   6 | 2015-12-22 16:06:49 |
| *12 |   1 |   5 | 2015-12-22 16:10:21 |
+-----+-----+-----+---------------------+
12 rows in set (0.00 sec)

PHP

mysql_connect("localhost", xxxx, xxxx);
mysql_select_db(performanceData);
mysql_set_charset("utf8");

//Assert provided device configuration exists
$deviceSwitch = false;
$deviceNum;
$configSwitch = false;
$configNum;
$errorSwitch = false;

$returnArray = array("testID"=>-1, "error"=>"NULL");
$errorString = "ERROR";

$deviceInfo = mysql_query("SELECT d.did AS 'did', d.name AS 'name', c.cid AS 'cid', c.lunType AS 'lunType' FROM device d JOIN config c ON d.did = c.did;");

while ($row = mysql_fetch_assoc($deviceInfo))
{
    if (strcmp($_GET["device"], $row["name"]) == 0)
    {
        $deviceSwitch = true;
        $deviceNum = $row["did"];
        if (strcmp($_GET["config"], $row["lunType"]) == 0)
        {
            $configSwitch = true;
            $configNum = $row["cid"];
        }
    }
}

if ($deviceSwitch && $configSwitch)
{
    if (mysql_query("INSERT INTO test (cid, did) VALUES (".$configNum.", ".$deviceNum.");"))
    {
        $returnArray["testID"] = mysql_insert_id();
    }
    else
    {
        $errorString .= " - Failed to insert into database, please contact sysadmin";
        $errorSwitch = true;
    }
}
else
{
    $errorSwitch = true;
    $errorString .= " - Improper device or config formatting";
}

if ($errorSwitch)
    $returnArray["error"] = $errorString;

echo json_encode($returnArray);

?>

MySQL 表设置

CREATE TABLE test(
    tid int NOT NULL AUTO_INCREMENT,
    cid int NOT NULL,
    did int NOT NULL,
    runtime TIMESTAMP
        DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (tid),
    FOREIGN KEY (cid) REFERENCES config (cid)
        ON DELETE CASCADE,
    FOREIGN KEY (did) REFERENCES device (did)
        ON DELETE CASCADE
);

编辑:经过进一步测试后,我发现这只是一个问题,直到我的数据库自动递增的 ID 达到值 7,然后它才能正常运行。

编辑 2:这似乎是由浏览器预取引起的,因为我使用相同的 URL 足够多次,浏览器将其记录在“访问最多的页面”中。当打开一个新选项卡时,它会预取 URL,将不需要的行添加到数据库中。暴露直接到数据库的 GET Web API 的一个关键弱点。

编辑 3:我决定 force CLI interaction解决这个问题。这消除了问题,但不允许对脚本进行基于 URL/GET 的访问。

最佳答案

CLI 可用于避免您不希望 Web 在您的应用程序上调用操作的情况。您可以使用 Symphony Console packagist 上提供的组件,可以使用 composer 轻松集成。

如果您不希望严格执行 CLI,您可以从 header 值中检测预取请求并忽略这些请求。

Chrome/Safari 发送 X-Purpose: preview 并且 Mozilla 在预取网页时发送 X-moz: prefetch HTTP header 。

关于PHP API脚本两次插入MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34426598/

相关文章:

php - 想要在页面重新加载和页面刷新时显示新上传的图像

php - 重定向 HTTP POST

php - html 清理造成困难

php - 如何列出 MSSQL 中的所有表?

php - 插入 YouTube 链接并仅显示 ID

php - 如何存储地点之间的距离?

php - 计算mysql中的所有原始值

php - 从 .html 页面重定向未登录的用户?

php - 我如何获得用户名? (关系数据库)

mysql - AWS Django Elastic Beanstalk 迁移错误