PHP MySQL仅在IP是新的时才更新列 [英] Php MySQL updating a column only if IP is new

查看:59
本文介绍了PHP MySQL仅在IP是新的时才更新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的脚本在每次新IP时将视图计数更新+1. 并且在604800秒之后,如果同一用户(相同IP)在604800秒后再次回来观看,则观看次数将增加1. 有人可以帮我吗?

I'm trying to make my script update the view count by +1 everytime a IP is new. and after 604800 seconds, if the same user(same IP) comeback again after 604800 seconds view count by+1. Can someone help me out here.

//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' ");
 }}

推荐答案

不是完整的代码,仅供参考!

提示:两张桌子可能更好,一个保存视频信息,另一个保存访客信息

is not a full code,just for your information!

Tips:two tables maybe better,one save video info,another save visitor info

videos:id,ip,name,title,views,update_time

// first, select $video info and get the result --- "select * from videos where ip='{$ip}' and id='{$id}'";
// we got $video look like : array('id'=>1,'update_time'=>'1453970786') 

$time = time();
if( isset($video['id']) && (($time - $video['update_time']) >= 604800) ){
	$pdo->query("UPDATE `videodb`.`videos` SET `views`=`views`+1 AND update_time='$time' WHERE id = '$id'");
}elseif( !isset($video['id']) ){
	$pdo->query("INSERT INTO `videodb`.`videos`( `ip`, `name`, `title`, `views`) VALUES ('$ip', '$id', '$videoName', 1)");
}

修改

$pdo->query("INSERT INTO `videodb`.`videos`(`id`, `ip`, `name`, `title`, `views`) VALUES ('$id','$ip', '$id', '$videoName', 1)");

这篇关于PHP MySQL仅在IP是新的时才更新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆