我想使用下拉列表来排序查询结果。无论在下拉列表中选择何种选项,查询都将按所选选项的升序排序。我的查询工作正常,我只需要包含我知道在WHERE子句之后的ORDER BY子句。请仔细查看表格,请帮我解决这个问题。
以下是代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Exam Interface</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="exam_interface.php" method="post" name="sessionform"> <!-- This will post the form to its own page"-->
<p>Session ID: <input type="text" name="sessionid" /></p> <!-- Enter Session Id here-->
<p>Module Number: <input type="text" name="moduleid" /></p> <!-- Enter Module Id here-->
<p>Teacher Username: <input type="text" name="teacherid" /></p> <!-- Enter Teacher here-->
<p>Student Username: <input type="text" name="studentid" /></p> <!-- Enter User Id here-->
<p>Grade: <input type="text" name="grade" /></p> <!-- Enter Grade here-->
<p>Order Results By: <select name="order">
<option name="noorder">Don't Order Results</option>
<option name="ordersessionid">Session ID</option>
<option name="ordermoduleid">Module Number</option>
<option name="orderteacherid">Teacher Username</option>
<option name="orderstudentid">Student Username</option>
<option name="ordergrade">Grade</option>
</select>
<p><input type="submit" value="Submit" /></p>
</form>
<?php
$username="xxx";
$password="xxx";
$database="mobile_app";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$sessionid = $_POST['sessionid'];
$moduleid = $_POST['moduleid'];
$teacherid = $_POST['teacherid'];
$studentid = $_POST['studentid'];
$grade = $_POST['grade'];
$result = mysql_query("SELECT * FROM Module m INNER JOIN Session s ON m.ModuleId = s.ModuleId JOIN Grade_Report gr ON s.SessionId = gr.SessionId JOIN Student st ON gr.StudentId = st.StudentId WHERE ('$sessionid' = '' OR gr.SessionId = '$sessionid') AND ('$moduleid' = '' OR m.ModuleId = '$moduleid') AND ('$teacherid' = '' OR s.TeacherId = '$teacherid') AND ('$studentid' = '' OR gr.StudentId = '$studentid') AND ('$grade' = '' OR gr.Grade = '$grade')");
$num=mysql_numrows($result);
echo "<table border='1'>
<tr>
<th>Student Id</th>
<th>Forename</th>
<th>Session Id</th>
<th>Grade</th>
<th>Mark</th>
<th>Module</th>
<th>Teacher</th>
</tr>";
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['StudentId'] . "</td>";
echo "<td>" . $row['Forename'] . "</td>";
echo "<td>" . $row['SessionId'] . "</td>";
echo "<td>" . $row['Grade'] . "</td>";
echo "<td>" . $row['Mark'] . "</td>";
echo "<td>" . $row['ModuleName'] . "</td>";
echo "<td>" . $row['TeacherId'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
</body>
</html>
我尝试过使用一个很好的发布给我的开关声明,但我无法让它工作。这就是我在下面尝试做的事情:
$orderfield = isset ($_POST['order']);
switch ($orderfield) {
case 'ordersessionid': $orderfield = 'gr.SessionId';
break;
case 'ordermoduleid': $orderfield = 'm.ModuleId';
break;
case 'orderteacherid': $orderfield = 's.TeacherId';
break;
case 'orderstudentid': $orderfield = 'gr.StudentId';
break;
case 'ordergrade': $orderfield = 'gr.Grade';
break;
}
$result = mysql_query("SELECT * FROM Module m INNER JOIN Session s ON m.ModuleId = s.ModuleId JOIN Grade_Report gr ON s.SessionId = gr.SessionId JOIN Student st ON gr.StudentId = st.StudentId WHERE ('$sessionid' = '' OR gr.SessionId = '$sessionid') AND ('$moduleid' = '' OR m.ModuleId = '$moduleid') AND ('$teacherid' = '' OR s.TeacherId = '$teacherid') AND ('$studentid' = '' OR gr.StudentId = '$studentid') AND ('$grade' = '' OR gr.Grade = '$grade') ORDER BY '$orderfield' ASC");
任何帮助都会受到很多关注。谢谢
答案 0 :(得分:2)
尝试
$orderfield = isset ($_POST['order']) ? $_POST['order'] : 'default order field';
请参阅http://php.net/manual/en/function.isset.php
注意到...... ORDER BY
语句的参数不是参数,而是表达式。换句话说,在查询中丢失$orderfield
周围的单引号,即
... ORDER BY $orderfield ASC");
答案 1 :(得分:2)
看起来您可能在这些选项上添加了错误的属性。你想要价值,而不是名字:
<option value="noorder">Don't Order Results</option>
找出它无法正常工作的原因:
$orderfield = isset ($_POST['order']);
switch ($orderfield) {
case 'ordersessionid': $orderfield = 'gr.SessionId';
break;
case 'ordermoduleid': $orderfield = 'm.ModuleId';
break;
case 'orderteacherid': $orderfield = 's.TeacherId';
break;
case 'orderstudentid': $orderfield = 'gr.StudentId';
break;
case 'ordergrade': $orderfield = 'gr.Grade';
break;
}
// Debug here by checking the value of $orderfield
// This will tell us if the switch statement did its job
var_dump($orderfield);
// Alternatively, you could set a default case and catch it there
在发生这一切之前,您还可以检查$_POST
数组。看一看,看看你的所有表格值是否都在那里:
var_dump($_POST);
die();