摇动投票
随着数据库的加入,是时候将用户所看到的页面整合起来了。这些页面中的第一个是user.php文件,其连接到数据库以从中取得最新的投票问题然后和它所有可能的回答一起显示。请看:
<html> <head><basefont face = 'Arial'></head> <body> <?php // include configuration file include('config.php'); // open database connection $connection = mysql_connect($host, $user, $pass) or die('ERROR: Unable to connect!'); // select database mysql_select_db($db) or die('ERROR: Unable to select database!'); // generate and execute query $query = "SELECT qid, qtitle FROM questions ORDER BY qdate DESC LIMIT 0, 1"; $result = mysql_query($query) or die("ERROR: $query.".mysql_error()); // if records are present if (mysql_num_rows($result) > 0) { $row = mysql_fetch_object($result); // get question ID and title $qid = $row->qid; echo '<h2>'.$row->qtitle .'</h2>'; echo "<form method = post action = 'user_submit.php'>"; // get possible answers using question ID $query = "SELECT aid, atitle FROM answers WHERE qid = '$qid'"; $result = mysql_query($query) or die("ERROR: $query.".mysql_error()); if (mysql_num_rows($result) > 0) { // print answer list as radio buttons while ($row = mysql_fetch_object($result)) { echo "<input type = radio name = aid value = '".$row->aid."'>'".$row->atitle."'</input><br />"; } echo "<input type = hidden name = qid value = '".$qid."'>"; echo "<input type = submit name = submit value = 'Vote!'>"; } echo '</form>'; } // if no records present, display message else { echo '<font size="-1">No questions currently configured</font>'; } // close connection mysql_close($connection); ?> </body> </html>
请特别注意我正在运行的SQL查询:我正在使用ORDER BY、DESC和LIMIT关键字来确保我从问题表格中得到了最新的记录(问题)。一旦查询返回一条结果,记录ID用于从答案表格中获得其对应的答案列表。然后用一个while()循环以一系列单选按钮来打印答案。对应于每个答案的记录ID附着其单选按钮;当表单被递交时,这个标识符用于确保正确的计数器得到更新。
请注意,如果数据库是空的,那么将会显示一条错误消息。在这个例子中,我们已经在数据库中插入了一条问题,因此你根本不会看见错误;然而,确保所有可能发生的事情都得到解决是良好的编程习惯,即使是不经常发生的事情。
包含在脚本顶部的config.php文件包含了对MySQL数据库的访问参数。该数据被放置在独立的文件中,这使得当你将应用程序移到一个新的服务器上的时候容易改变该数据。请仔细看下面的代码:
<?php
// database access parameters
$host = 'localhost';
$user = 'guest';
$pass = 'guessme';
$db = 'db3';
![]()
?>
下面是表单看上去的样子:
好的,现在你已经将投票展现出来了。用户正在排队参与投票而且产生了数百万的点击。你能用它们来做什么呢?
答案存在于当用户投票或者递交之前描述的表单时所激活的脚本中。该脚本(user_submit.php)负责为适当的问题/答案组合更新投票计数器。请看:
<html> <head><basefont face = 'Arial'></head> <body> <?php if (isset($_POST['submit'])) { if (!isset($_POST['aid'])) { die('ERROR: Please select one of the available choices'); } // include configuration file include('config.php'); // open database connection $connection = mysql_connect($host, $user, $pass) or die('ERROR: Unable to connect!'); // select database mysql_select_db($db) or die('ERROR: Unable to select database!'); // update vote counter $query = "UPDATE answers SET acount = acount + 1 WHERE aid = ".$_POST['aid']." AND qid = ".$_POST['qid']; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); // close connection mysql_close($connection); // print success message echo 'Your vote was successfully registered!'; } else { die('ERROR: Data not correctly submitted'); } ?> </body> </html>
该脚本首先通过验证答案ID $_POST['aid']的存在性以检查确认一个答案已经得到选择。假设该ID是存在的,那么脚本更新数据库以反映新的投票然后显示一条适当的消息。
现在,返回浏览你的笔记本然后看看最初的需求列表。是的,你可以除去1#条目。向前继续2#条目…
