gpt4 book ai didi

PHP MySQL 仅当 IP 是新时才更新列

转载 作者:行者123 更新时间:2023-11-29 21:33:50 27 4
gpt4 key购买 nike

我试图让我的脚本在每次 IP 出现新值时将观看次数更新 +1。604800秒后,如果同一用户(同一IP)在604800秒后再次回来,则查看计数+1。有人可以帮我吗?

//Get video id
$id = $_GET['id'];
//Get video title
$videoName = $_GET['idtitle'];



//Connect to database
$pdo = new PDO('mysql:host=localhost;dbname=videodb', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Get user IP
$ip = $_SERVER["REMOTE_ADDR"];

//Insert that user IP, Name, Title and increase view count by +1
//This is what i want to accomplish but is not working!
$insert = $pdo->query("INSERT INTO `videodb`.`videos` ( `ip`, `name`, `title`, `views`) VALUES ('$ip', '$id', '$videoName', `views`+1)");

//示例二

//Select the IP from videos
$select = $pdo->prepare("SELECT `ip` FROM `videos`");
$sql = $select->execute();
while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
//If the IP in database is not equal to the user IP in database views +1
if($row->fetch('ip') == $ip){
$pdo->query("UPDATE videos SET `views` = `views`+1 WHERE id = '$id' ");
}}

最佳答案

在表格中再添加一列,作为“last_viewed_at DATETIME”。

现在在将记录插入数据库之前:

当您查看最近7天的记录时,请按如下查询:

$row = "SELECT * FROM videos WHERE 
id = 'users.ip.to.check'";

条件:您已获得users.ip.to.check记录现在检查时差是否超过7天。意思是。

if (count($row) > 0) {
$ipLastViewAt = new \DateTime($row['last_viewed_at']);
$currentDate = new \DateTime();

$diff = $currentDate->diff($ipLastViewAt)->format("%a");
if ($diff >= 7 ) {
// update the record with the view + 1 and with last_view_at = currentDateTime;
}
} else {
// Insert the record with the view +1 and with last_view_at = currentDateTime;
}

关于PHP MySQL 仅当 IP 是新时才更新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35055528/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com