我有以下输出:
12345: 1
1234: 7
1234: 7
1234: 6
1234: 4
它是从以“?id = 12345”结尾的URL生成的。应该发生的是当数据库中存在id时,id会增加1,否则会增加id。 1234产生的最后一个结果是正确的,但是如何停止生成1234的先前结果?
while ($row = $database->row()->fetch()) {
$id = $row["id"];
$count = $row["count"];
if ($id != $_GET["id"]) {
$insert = new MySQLConnect();
$insert->query("INSERT INTO links VALUES (:id, 1);");
$insert->run(array("id" => $_GET["id"]);
} elseif ($id == $_GET["id"]) {
$update = new MySQLConnect();
$update->query("UPDATE links SET count=count + 1 WHERE id=:id;");
$update->run(array("id" => $_GET["id"]);
}
}
编辑:
function display_data()
{
$display = new MySQLConnect();
$display->query("SELECT * FROM links;");
$display->run();
while ($row = $display->row()->fetch()) {
$id = $row["id"];
$count = $row["count"];
$GLOBALS["output"] .= $id . ": " . $count . "<br/>";
}
}
答案 0 :(得分:0)
如果您只想创建/更新作为参数
发送的ID,则不需要“while”$useId = (int)$_GET["id"];
$insert = new MySQLConnect();
$insert->query("INSERT INTO `links` (`id`, `count`) VALUES ('$useId', 1) ON DUPLICATE KEY UPDATE `count` = `count` + 1");
$insert->run();