前端php页面
<?php if(isset($_GET['id'])){ $id=$_GET['id']; } include('data/conn.php'); $sqls="select count from activity where id=$id"; $results=mysqli_query($link,$sqls); $row=mysqli_fetch_assoc($results); $ip= $_SERVER['REMOTE_ADDR']; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="css/saveclick.css" type="text/css" /> <script type="text/javascript" src="js/jquery.js"></script> <title>分享收集花瓣</title> </head> <body> <div class="content"> <div class="word-2"></div> <div class="ticket"></div> <div class="count" id="count"><?php echo $row['count'] ?></div> <div class="font">花瓣数量</div> <div style="float: right;" onclick="collect();"> <div class="huaban"></div> <div class="font ft-1" >送TA花瓣</div> </div> <div class="st" style="display:none;"><?php echo $id ?></div> <div class="ip" style="display:none;"><?php echo $ip;?></div> </div> </body> <script type="text/javascript"> function collect(){ var id=$('.st').text(); var count=$("#count").text(); var ip=$('.ip').text(); $.post('data/data-count.php',{"id":id,"ip":ip},function(data){ if(count>=10){ alert("攒花成功了,赶紧截图帮TA发到天甲网络微信公众号领奖去吧!"); window.location.href="ticket1.php"; }else{ if(data['msg']==2){ alert("你已经送过TA花瓣了,分享到朋友圈让其他小伙伴一起为TA送花!"); }else if(data['msg']==0){ alert('送TA花瓣失败了,请重新送花!'); }else{ alert("送花成功,赶紧分享到朋友圈喊其他小伙伴一起为TA送花吧!"); $("#count").html(parseInt(count)+1); } } },'JSON'); } </script> </html>
数据库连接mysqli
<?php $link=mysqli_connect('localhost','root','','38activity'); mysqli_query($link,"SET NAMES 'utf8'"); ?>
后台处理数据页面实现计数更新以及ip防止重复记录
<?php include('conn.php'); $id=$_POST['id']; $ip=$_POST['ip']; $ip_sql=mysqli_query($link,"select ip from ip where aid=$id and ip='$ip' "); $count=mysqli_num_rows($ip_sql); if($count==0){ //没有投票 $sql="update activity set count=count+1 where id=$id" ; mysqli_query($link,$sql); $sql_in = "insert into ip(ip,aid) values ('$ip','$id')"; mysqli_query($link,$sql_in); if(mysqli_insert_id($link)>0){ $arr['msg'] = 1; $arr['success'] = '投票成功,谢谢您的参与'; echo json_encode($arr); }else{ $arr['msg'] = 0; $arr['success'] = '操作失败,请重试'; echo json_encode($arr); } }else{ $arr['msg'] = 2; $arr['success'] = '您已经投票过了'; echo json_encode($arr); }
数据库表