dousi4148 2016-11-20 15:41
浏览 39
已采纳

如何从表格中选出具有最多选票的候选人姓名[关闭]

This is my php code of webpage. I want to retrieve the candidate's name as well as number of votes. But I don't know how to. As you can see I have tried a lot but no use. My database name is election, my table name is votes. In table I have 3 columns id, name, votes, and only three candidate are inserted (not more than that).

<html>
<head>
<title>RESULT</title>
<script type="text/javascript" src="http://localhost/WTL/js/validateLogin.js">
</script>
<style type="text/css">
.btn{

    border-radius:15px;
    border:solid 2px #B22222;
    padding:5px;
    width:10%;
    height:8%;
    background-color:white;
    float:center;

}

</style>

</head>
<body>
 <div class="header">
<img src="http://localhost/WTL/images/home2.jpg" id="home" width="1350" height="180">
</div>
<div name="header2">
<form name="result" method="post" action="">
<Table class="tabb" align="center" >
<th>
<font size="5px" color="#B22222">RESULT</font>
</th>

   <tr><td><input type="textarea" rows="6" cols="50" width="50%" height="50%" name="resultDisplay" placeholder="click result to check the result" class="textarea"></td></tr>
        </table>
        <center><div name="header3" class="last"><input type="submit" name="sub" value="RESULT" onclick="validate();" class="btn" ></div></center>
</form>
</div>
</body>
</html>
<?php
session_start();
$display= "";

if(isset($_REQUEST['sub']))
{
$db = mysqli_connect("localhost","root","","election");
if($db->connect_error)
{
    die("connection failed".$db->connect_error);

}   
else
{
    $sql="SELECT name,votes FROM cr
        WHERE votes = (SELECT Max(votes) FROM cr)";
    $res=mysqli_query($db,$sql);
    //$name=$res['name'];
    //$votes=$res['votes'];
    echo '<textarea>', $res , '</textarea>';
    //$display .= '<div>'.$name.' '.$votes.'</div>';
}   
}
?>
  • 写回答

2条回答 默认 最新

  • doudou20080720 2016-11-20 15:51
    关注

    The original question contain many mistakes which mostly seems to have segnalated by the comments to the question and other answers this response is limited to only part of the code which relates to the display of results

    for get the result you should use a loop (assuming that you have not errors) for access to each row array and proper index for get the each column

     while ($row = mysqli_fetch_array($res))
        {
    
             echo '<textarea>'. $row['name'] .'</textarea><br/>';
             echo '<textarea>'. $row['votes'] .'</textarea><br/>';
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。