【发布时间】:2017-04-04 00:12:12
【问题描述】:
这是我的网页 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>';
}
}
?>
【问题讨论】:
-
“救命!紧急” - 哦,我明白了。顺便说一句,您不能使用
$res查询的“回声”。 -
你为什么不按票数降序排列,限制为 1?
SELECT name,votes FROM cr ORDER BY votes DESC LIMIT 1- 更清楚一点。 -
您需要“循环”成功的结果。你不能只是
echo $res那样。您需要while或foreach。这一切都在这个php.net/manual/en/mysqli.query.php的手册中,网上也有很多教程。 -
你也很有可能在标题之前输出
session_start();的位置 - 这应该高于输出,错误报告会触发标题发送警告 php.net/manual/en/function.error-reporting.php - 如果你不是分配任何会话数组,然后您可以安全地删除它。 -
<input type="textarea"这也是无效的。您的代码包含太多错误。这个JS函数又在哪里validate()?