每次我点击视频链接来测试我的脚本 而不是将+1添加到相同的视频名称aka(id)。 它只是不断添加列。 如何阻止MySQL添加更多colums并只更新视图+1。
//Get video id
$id = $_GET['id'];
//Get video title
$videoName = $_GET['idtitle'];
//Connect to DB
$pdo = new PDO('mysql:host=localhost;dbname=videodb', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Reset the ips on database after 7 days? correct me if i'm wrong here
const HIT_OLD_AFTER_SECONDS = 604800; // Value 1 week.
//Check if connection fail or pass
//if(!$pdo){echo "Fail!";} else {echo "Pass!";}
//Insert the $id, $videoName, and views 1
$pdo->query("INSERT INTO `videodb`.`videos` (`name`, `title`, `views`) VALUES ('$id', '$videoName', 1)");
//This is where i'm stuck
//Select name from the database videos table
//If the name inside video table &&(AND) $videoName are equal(TRUE)
//Update views to views +1
if($pdo->query("SELECT `name` FROM `videodb`.`videos`") && $videoName == true ){
$pdo->query("UPDATE `videos` SET `views` = `views`+1");
}
答案 0 :(得分:1)
其他任何事情
$id = $_GET['id'];
$pdo->query("INSERT INTO `videodb`.`videos` (`name`, `title`, `views`) VALUES ('$id', '$videoName', 1)");
易受SQL注入攻击。
前进,
"UPDATE `videos` SET `views` = `views` + 1 WHERE `id` = '$id'"
如果您在页面加载时运行该SQL语法,它将更新您的视图列+1。
但是,对于重复的IP,您需要将“ip”列设置为键和
"INSERT INTO `videos` (params) VALUES (params) ON DUPLICATE KEY UPDATE `views` = views+1"