2011-10-20 58 views
0

當我按提交按鈕來提交表單時,我的查詢結果中沒有顯示任何記錄。我不知道這是爲什麼。我知道這個查詢是正確的,因爲我之前已經測試過它,但是當我包含WHERE子句時它不起作用,但是我相信這是在根據表單中輸入內容檢索行時正確的代碼。它沒有爲我的查詢結果輸出任何行,這是爲什麼?

另外我想顯示作爲AnswerContent的數組StudentAnswer,但是當我這樣做AnswerContent只顯示每個AnswerId。我如何做到爲每個StudentAnswer顯示? StudentAnswer與AnswerId相同,因爲無論選擇哪個答案,它都會通過其ID檢索答案並將其存儲在StudentAnswer字段中。請幫忙。

下面是我的代碼:

<!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 Q & A</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    </head> 

    <body> 
    <?php 

    $username="xxx"; 
    $password="xxx"; 
    $database="mobile_app"; 

    mysql_connect('localhost',$username,$password); 
    @mysql_select_db($database) or die("Unable to select database"); 

    foreach (array('sessionid','questionno','studentid','orderfield') as $varname) { 
    $$varname = (isset($_POST[$varname])) ? $_POST[$varname] : ''; 
    } 

    switch ($orderfield) { 
    case 'orderquestionno': 
     $orderfield = 'q.QuestionNo'; 
     $ordername = 'Question Number'; 
     break; 
    case 'orderstudentid': 
     $orderfield = 'sa.StudentId'; 
     $ordername = 'Student Username'; 
     break; 
    case 'orderwhole': 
     $orderfield = 'q.SessionId AND q.QuestionNo'; 
     $ordername = 'Session ID and Question Number'; 
     break; 
    case 'ordersessionid': 
    default: 
     $orderfield = 'q.SessionId'; 
     $ordername = 'Session ID'; 
     break; 
    } 

?> 

<h1>MOBILE EXAM QUESTIONS AND ANSWERS SEARCH</h1> 
<p><strong>NOTE: </strong>If a search box is left blank, then the form will search for all data under that specific field</p> 

<form action="exam_QA.php" method="post" name="sessionform">  <!-- This will post the form to its own page"--> 
    <p>Session ID: <input type="text" name="sessionid" value="<?php echo $sessionid; ?>" /></p>  <!-- Enter Session Id here--> 
    <p>Question Number: <input type="text" name="questionno" value="<?php echo $questionno; ?>" /></p>  <!-- Enter Question Number here--> 
    <p>Student Username: <input type="text" name="studentid" value="<?php echo $studentid; ?>" /></p>  <!-- Enter User Id here--> 
    <p>Order Results By: 
    <select name="orderfield"> 
     <option value="ordersessionid"<?php if ($orderfield == 'q.SessionId') echo ' selected="selected"' ?>>Session ID</option> 
     <option value="orderquestionno"<?php if ($orderfield == 'q.QuestionNo') echo ' selected="selected"' ?>>Question Number</option> 
     <option value="orderstudentid"<?php if ($orderfield == 'sa.StudentId') echo ' selected="selected"' ?>>Student Username</option> 
     <option value="orderwhole"<?php if ($orderfield == 'q.SessionId AND q.QuestionNo') echo ' selected="selected"' ?>>Session ID and Question Number</option> 
    </select> 
    </p> 
    <p><input type="submit" value="Submit" name="submit" /></p> 
</form> 

<?php 
    if (isset($_POST['submit'])) { 

    $query = " 
    SELECT *, a2.AnswerContent as StudentAnswerContent 
    FROM Question q 
    INNER JOIN StudentAnswer sa ON q.QuestionId = sa.QuestionId 
    LEFT JOIN Answer a ON (sa.QuestionId = a.QuestionId AND a2.CorrectAnswer = 1) 
    LEFT JOIN Answer a2 ON (sa.QuestionId = a2.QuestionId AND a2.AnswerId = sa.StudentAnswer) 
     WHERE 
     ('".mysql_real_escape_string($sessionid)."' = '' OR q.SessionId = '".mysql_real_escape_string($sessionid)."') 
     AND 
     ('".mysql_real_escape_string($questionno)."' = '' OR q.QuestionNo = '".mysql_real_escape_string($questionno)."') 
     AND 
     ('".mysql_real_escape_string($studentid)."' = '' OR sa.StudentId = '".mysql_real_escape_string($studentid)."') 
     ORDER BY $orderfield ASC"; 

    $num = mysql_num_rows($result = mysql_query($query)); 
    mysql_close(); 

?> 

<p> 
    Your Search: 
    <strong>Session ID:</strong> <?php echo (empty($sessionid)) ? "'All Sessions'" : "'$sessionid'"; ?>, 
    <strong>Question Number:</strong> <?php echo (empty($questionno)) ? "'All Questions'" : "'$questionno'"; ?>, 
    <strong>Student Username:</strong> <?php echo (empty($studentid)) ? "'All Students'" : "'$studentid'"; ?>, 
    <strong>Order Results By:</strong> '<?php echo $ordername; ?>' 
</p> 
<p>Number of Records Shown in Result of the Search: <strong><?php echo $num ?></strong></p> 
<table border='1'> 
    <tr> 
    <th>Session ID</th> 
    <th>Question Number</th> 
    <th>Question</th> 
    <th>Correct Answer</th> 
    <th>StudentAnswer</th> 
    <th>Correct Answer Weight</th> 
    <th>Student Answer Weight</th> 
    <th>Student ID</th> 
    </tr> 
    <?php 

    while ($row = mysql_fetch_array($result)) { 

    if ($row['StudentAnswer'] == $row['AnswerId']) { $row['StudentAnswerWeight'] = $row['Weight%']; } else { $row['StudentAnswerWeight'] = '0'; } 
    $row['StudentAnswer'] == $row['AnswerContent']; 
     echo " 
    <tr> 
    <td>{$row['SessionId']}</td> 
    <td>{$row['QuestionNo']}</td> 
    <td>{$row['QuestionContent']}</td> 
    <td>{$row['AnswerContent']}</td> 
    <td>{$row['StudentAnswerContent']} </td> 
    <td>{$row['Weight%']}</td> 
    <td>{$row['StudentAnswerWeight']}</td> 
    <td>{$row['StudentId']}</td> 
    </tr>"; 
    } 
    ?> 
</table> 

<?php 
    } 
?> 

謝謝:)

+1

您是否嘗試過當你有clausole WHERE做的var_dump? –

+0

是的,我確實嘗試過沒有任何東西。在左邊加入之前,我做了內部連接並且它工作正常,但我被告知要這樣做, – BruceyBandit

+0

您必須提供更多信息才能幫助您。 –

回答

2

你的第一LEFT JOIN被引用的別名a2不在這一點上還不存在,因爲它是下一步加入LEFT JOIN

LEFT JOIN Answer a ON (sa.QuestionId = a.QuestionId AND a2.CorrectAnswer = 1) 
                 ^^ invalid 

我猜想a2的參考實際上應該是a。請注意,如果你有錯誤報告在你的開發環境不受開啓(error_reporting(E_ALL)在頁面的頂部),你會很容易趕上這一點,因爲你會試圖運行查詢時收到以下錯誤信息:

未知列 'a2.CorrectAnswer' 在 '關於條款'

示範:http://sqlize.com/8Wjawg6g4N

相關問題