从浏览器运行以下脚本后,我发现它将数据插入我的MySQL数据库两次.我只在我的代码中打了一个电话,一切似乎都井然有序.我在想这可能与在if语句中调用查询有关,但是我无法找到与此相关的任何内容.
我正在使用以下查询运行脚本:
http://businesstools.vmem.int/performance/api/start.php?device=7300&config=thin
Run Code Online (Sandbox Code Playgroud)
当我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)
Run Code Online (Sandbox Code Playgroud)
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);
?>
Run Code Online (Sandbox Code Playgroud)
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
);
Run Code Online (Sandbox Code Playgroud)
编辑:进一步测试后,我发现这只是一个问题,直到我的数据库自动递增ID达到7,然后它才能正常运行.
编辑2:这似乎是由浏览器预取引起的,这是因为我使用相同的URL足够时间让浏览器将其记录在"访问量最大的页面"中.打开新选项卡时,它会预取URL,将不需要的行添加到数据库中.暴露了直接数据库GET Web API的一个关键弱点.
编辑3:我决定强制CLI交互来解决这个问题.这消除了该问题,但不允许基于URL/GET的脚本访问.
CLI 可用于避免您不希望 Web 对您的应用程序调用操作的情况。您可以使用Packagist 上提供的Symphony Console组件,并且可以使用 Composer 轻松集成。
如果您不希望严格执行 CLI,您可以从标头值中检测预取请求并忽略这些请求。
Chrome/Safari在预取网页时发送 HTTP 标头,X-Purpose: preview而 Mozilla 则发送HTTP 标头。X-moz: prefetch
| 归档时间: |
|
| 查看次数: |
666 次 |
| 最近记录: |