<?php session_start(); include("conn/conn.php");
$data=date("Y-m-d h:i:s");
$ip=getenv("REMOTE_ADDR");
if ($_POST[Submit]==true){
	$tb_vote_id=$_POST[tb_vote_id];
	if ($_POST[tb_vote_id]==""){ 
		echo "<script>alert('您没有选择投票的内容!'); window.location.href='vote.php?vote_subject=$_POST[tb_vote_subject]';</script>";
	}else{
		$query="select * from tb_vote_odd_ip where tb_vote_subject='$_POST[tb_vote_subject]' and tb_vote_ip='$ip'";
		$result=mysql_query($query);
		$row=mysql_num_rows($result);
		if ($row>0){
 			echo "<script>alert('".$ip." 您不可以重复投票!'); window.location.href='vote.php?vote_subject=$_POST[tb_vote_subject]';</script>";
		}else{
 			$querys="update tb_vote_odd set tb_vote_counts=tb_vote_counts+1 where tb_vote_id='$tb_vote_id'";
 			$result=mysql_query($querys);

 			$query=mysql_query("select * from tb_vote_odd where tb_vote_id='$tb_vote_id'");
 			$myrow=mysql_fetch_array($query);
 			$tb_vote_subject=$myrow[tb_vote_subject];
 			$tb_vote_content=$myrow[tb_vote_content];
 			
			$querys="insert into tb_vote_odd_ip(tb_vote_subject,tb_vote_content,tb_vote_ip,tb_vote_date,tb_vote_id )values('$tb_vote_subject','$tb_vote_content','$ip','$data','$tb_vote_id')";
 			$result=mysql_query($querys);
 			echo "<script>alert('投票提交成功!'); window.location.href='vote.php?vote_subject=$_POST[tb_vote_subject]';</script>";
		}
	}
}


if ($_POST[Submit2]==true){ 
	if(empty($_COOKIE['vote_cookie'])==false){			//通过COOKIE限制重复投票
 		echo "<script>alert('您不可以重复投票，请在1小时后重新登录！'); window.location.href='vote.php?vote_subject=$_GET[more_subject]';</script>";
	}else{
		setcookie("vote_cookie",value,time()+3600);	//如果不存在,则创建COOKIE
 		$k=0;
    	while (list($name,$value)=each($_POST)){   
			$k+=$name;  
	    	if (is_numeric($name)==true){ 
            	mysql_query("update tb_vote_more set tb_vote_counts=tb_vote_counts+1 where tb_vote_id='".$name."'");          
	  		} 
		} 
		if ($k==0){
			echo "<script>alert('您不能投票');history.back();</script>"; exit;
		}
	  	mysql_query("insert into tb_vote_more_ip(tb_vote_subject,tb_vote_ip)values('$_GET[more_subject]','$ip')");
    	echo "<script>alert('投票提交成功!'); window.location.href='vote.php?vote_subject=$_GET[more_subject]';</script>";
 	}
}
?>