2011-10-14 49 views
0

我想用一個下拉列表訂購查詢結果選擇的值順序的表。無論在下拉列表中選擇了哪個選項,查詢都將按所選選項的升序排列。我幾乎在那裏,但問題是無論我從下拉菜單中選擇哪種情緒,它都只按會話ID排序。我怎樣才能讓其他下拉值工作,這樣當它們被選中時,表格將根據所選的值進行排序。請仔細查看錶格,switchstatment和ORDER BY子句,並請幫助我解決這個問題。by子句和switch語句使用順序嘗試根據從下拉菜單

下面是代碼:

<!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 value="noorder">Don't Order Results</option> 
<option value="ordersessionid">Session ID</option> 
<option value="ordermoduleid">Module Number</option> 
<option value="orderteacherid">Teacher Username</option> 
<option value="orderstudentid">Student Username</option> 
<option value="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 = isset ($_POST['sessionid']) ? $_POST['sessionid'] : ""; 
$moduleid = isset ($_POST['moduleid']) ? $_POST['moduleid'] : ""; 
$teacherid = isset ($_POST['teacherid']) ? $_POST['teacherid'] : ""; 
$studentid = isset ($_POST['studentid']) ? $_POST['studentid'] : ""; 
$grade = isset ($_POST['grade']) ? $_POST['grade'] : ""; 
$orderfield = isset ($_POST['order']) ? $_POST['order'] : 'default order field'; 

$sessionid = mysql_real_escape_string($sessionid); 
$moduleid = mysql_real_escape_string($moduleid); 
$teacherid = mysql_real_escape_string($teacherid); 
$studentid = mysql_real_escape_string($studentid); 
$grade = mysql_real_escape_string($grade); 

$_POST['ordersessionid'] = 'gr.SessionId'; 

$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; 
} 

echo $orderfield; 

$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"); 

$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> 

非常感謝你,請您幫。

+0

什麼問題/錯誤,您現在所面臨..? – Chandresh

回答

0

這是因爲你定義$orderfieldisset ($_POST['order'])isset()返回一個布爾值;它不返回它的參數的值,即使實際上定義了用作參數的變量。如果您更改該行看起來更像是涉及isset()的其他行,那麼您將可以使用它。

$orderfield = isset ($_POST['order']) ? $_POST['order'] : ""; 

爲了安全起見,我建議另外兩個步驟:

  • 一個default情況下的情況下,添加到您的交換機所提交order場是不是你預期值的一個
  • 使用不同的命名將查詢中的變量變爲用於包含POST值的變量,以確保您在查詢中放入的內容只是您已清理過的內容或您自己製作的內容。
+0

值得指出的是,在'order'字段中使用字符串轉義函數將不會在這裏安全,因爲您不在查詢中包含它的內容作爲字符串文本的一部分(在引號中)。 – Hammerite

+0

工作一個魅力,謝謝 – BruceyBandit

0

這條線是你的問題:

$orderfield = isset ($_POST['order']); 

要設置要切換的變量,是真的還是假的。

0

你正面臨某種問題,因爲只有你的代碼.. :)

$orderfield = isset ($_POST['order']); // Replace this line with below code ; 


$orderfield = ""; 
if(isset($_POST['order'])){ 

    $orderfield = $_POST['order']; 
} 

剛剛嘗試這一點code..i認爲你會得到出關的問題。

謝謝。

+0

想通了感謝Hammerite,但你是正確的它是一段代碼,擰我 – BruceyBandit