如何从表中检索具有投票数的最高投票候选人姓名

时间:2016-11-20 15:41:43

标签: php mysql xampp

这是我的网页php代码。我想检索候选人的姓名和投票数。但我不知道该怎么做。正如你所看到的,我已经尝试了很多但没有用。我的数据库名称是选举,我的表名是投票。在表格中,我有3列id,name,votes,并且只插入了三个候选者(不超过那个)。

<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 个答案:

答案 0 :(得分:-3)

您的sql语法中有错误。你的代码应该是这样的:

<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
$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, Max(votes) as vote FROM cr group by name";
    $res=mysqli_query($db,$sql);
    //$name=$res['name'];
    //$votes=$res['votes'];
    echo '<textarea>'.$res['name'].' - '.$res['vote'].'</textarea>';
    //$display .= '<div>'.$name.' '.$votes.'</div>';
}   
}
?>

答案 1 :(得分:-3)

原始问题包含许多错误,这些错误似乎主要通过对问题的评论进行了细分,而其他答案的回答仅限于与结果显示相关的部分代码

获取结果你应该使用一个循环(假设你没有错误)访问每个行数组和适当的索引来获取每一列

 while ($row = mysqli_fetch_array($res))
    {

         echo '<textarea>'. $row['name'] .'</textarea><br/>';
         echo '<textarea>'. $row['votes'] .'</textarea><br/>';
    }