当前位置:Gxlcms > PHP教程 > php实现投票系统的示例代码分析

php实现投票系统的示例代码分析

时间:2021-07-01 10:21:17 帮助过:19人阅读

投票分类


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <?php$db= new Mysqli("localhost","root","root","db_0808");$sql= "select * from fenlei";$result=$db->query($sql);$data_c= $result->fetch_all();?>
  9. <table border="1">
  10. <?php foreach ($data_c as $i){ echo" <tr><th><a href='vote.php?id={$i[0]}&is_radio={$i[2]}'>{$i[1]}</a></th></tr>";
  11. }?>
  12. </table>
  13. </body>
  14. </html>

投票界面


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <?php$id=$_GET['id'];$is_radio=$_GET['is_radio'];$db= new Mysqli("localhost","root","root","db_0808");$sql= "select * from toupiao WHERE category_id='{$id}'";$result=$db->query($sql);$data=$result->fetch_all();?>
  9. <form action="tongji.php" method="post">
  10. <table>
  11. <tr>
  12. <td>名字</td>
  13. <td>投票</td>
  14. </tr>
  15. <?php if ($is_radio==1){ foreach ($data as $i){ echo "<tr>
  16. <td>{$i[1]}</td>
  17. <td><input type='radio' name='r' value='{$i[0]}'></td>
  18. </tr>";
  19. }
  20. }else{ foreach ($data as $i){ echo "<tr>
  21. <td>{$i[1]}</td>
  22. <td><input type='checkbox' name='r[]' value='{$i[0]}'></td>
  23. </tr>";
  24. }
  25. } ?>
  26. </table>
  27. <input type="submit" value="提交">
  28. </form>
  29. </body>
  30. </html>

投票处理


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <table>
  9. <tr>
  10. <td>名字</td>
  11. <td>票数</td>
  12. </tr>
  13. <?php//$db= new Mysqli("localhost","root","root","db_0808");require_once "../ZY/YIN.class.php";$db = new YIN();$ip=$_SERVER['REMOTE_ADDR'];//获取ip$sql="select count(*) from table2 where ip='{$ip}'";$data = $db->StrQuery($sql);if($data!='0'){ echo "每个人一天只能投一次";$r=$_POST['r']; if (is_array($r)){ $s=implode(',',$r); $sq="select * from toupiao where id in ({$s})";
  14. }else{ $sq="select * from toupiao where id = {$r}";
  15. } $qurle=$db->Query($sq); foreach ($qurle as $i){ echo "<tr><td>{$i[1]}</td><td>{$i[2]}</td></tr>";
  16. }
  17. }else{ $sql = "INSERT INTO table2 VALUES (null,'{$ip}',DEFAULT)"; $squel=$db->strQuery($sql,0); if($squel){// $upsql=update table_name set votes = votes + 1 where ...
  18. //然后把数据insert into 到table2中$r=$_POST['r'];if (is_array($r)){ $s=implode(',',$r); $sql="update toupiao set count=(count+1) where id in ({$s})";
  19. }else{$sql="update toupiao set count=(count+1) where id = {$r}";
  20. }$data=$db->query($sql,0);if ($data){//echo "success";
  21. if (is_array($r)){ $s=implode(',',$r); $sq="select * from toupiao where id in ({$s})";
  22. }else{ $sq="select * from toupiao where id = {$r}";
  23. } $qurle=$db->Query($sq); foreach ($qurle as $i){ echo "<tr><td>{$i[1]}</td><td>{$i[2]}</td></tr>";
  24. }
  25. }
  26. }
  27. }?>
  28. </table>
  29. </body>
  30. </html>

以上就是php实现投票系统的示例代码分析的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行