$ip = getenv('REMOTE_ADDR');//得到ip
$time = date('Y-m-d');//得到当前日期
$query2=mysql_query("select * from user where ip='$ip'");
$row=mysql_fetch_array($query2);
if($time==$row[time]){
echo "<script language=javascript>alert('一天只能投一票');window.location.href='index.php';</script>";
}else{
mysql_query("INSERT INTO `vote`.`user` (`id` ,`time` ,`ip`,`picid` )VALUES (null, now(), '$ip', '$_GET[id]')");
$query=mysql_query("select * from pic where id=$_GET[id]");
$result=mysql_fetch_array($query);
$votes=$result[votes];
if($result){
$toupiao = "update pic set votes = $votes+ 1 where id =$_GET[id]";
mysql_query($toupiao);
echo "<script language=javascript>alert('投票成功');window.location.href='index.php';</script>";
}else{
echo "<script language=javascript>alert('投票失败');window.location.href='index.php';</script>";
}
}

解决方案 »

  1.   

    pic表里放着所需要投票的目标,票数也在这里面。
    vote表里放着投票信息,包括时间和ip
    大体就是一个ip一天只能投一票我知道肯定是$time==$row[time]这句出了问题,$row里面是个数组,不能用$time跟一个数组进行比较。但是我的想法是把先通过ip为条件把这个ip投过票的时间取出来,再通过时间判断是不是今天投的票。这是我想到的一个笨方法,不知道怎么实现。或者欢迎有经验的老鸟们拍砖指导。如果我这样写的不合理的话重新写就好了。不过这是我能想到的验证是否相同ip同天投票的办法了。
      

  2.   

    $ip=$_SERVER['REMOTE_ADDR'];//获取ip
    $sql="select * from table2 where ip=$ip and time=".date('Ymd');
    $res=$db->getRow($sql);
    if($res){
      echo "每个人一天只能投一次";
    }else{
     $upsql=update table_name set votes = votes + 1 where ...
     }这是另外一个朋友发给我的。但是这样写的话会报错(按照他的思路用普通的php写出来,不是照搬),如果$res不存在的话那么就会报错mysql_fetch_array没有返回值。
      

  3.   


    $query2=mysql_query("select * from user where ip='$ip' order by time desc");
    这个是我现在的解决办法,不过貌似有点不合理了
      

  4.   

    select id from 投票表 where 投票ip=当前ip and  (投票时间戳< 当前天+1天) and (投票时间戳>当前天-1天)如果能取到值说明已经投过票,不让投,否则add一票
      

  5.   

    第一段程序如果没得记录,取不到$row[time]的值,会出错。
    $query2=mysql_query("select * from user where ip='$ip'");
    $row=mysql_fetch_array($query2);
    if($time==$row[time]){
    改成:
    $query2=mysql_query("select count(*) from user where ip='$ip'" and time='.$time.');
    $row=mysql_fetch_array($query2);
    if (mysql_num_rows($row)==0){
    这样应该可以了。
      

  6.   

    $ip = getenv('REMOTE_ADDR');//得到ip
    $time = date('Y-m-d');//得到当前日期
    $query2=mysql_query("select * from user where ip='$ip' and `time`='$time'");注意:time是mysql的保留字,最好换一个