2011-10-20 2 views
0

제출 단추를 눌러 양식을 제출하면 내 조회 결과에 레코드가 표시되지 않습니다. 나는 이것이 왜 있는지 모른다. 이전에 테스트했지만 WHERE 절이 포함되어 있어도 쿼리가 올바르다는 것을 알고 있습니다.하지만 폼에 입력 된 내용에 따라 행을 검색하려고 할 때 올바른 코드라고 확신합니다.내 쿼리 결과에 대한 행이 출력되지 않습니다. 이유가 무엇입니까?

또한 StudentAnswer를 AnswerContent로 배열에 표시하려하지만이 경우 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)) 개발 envrionment에 켜져 있습니다 열 '절에'의 'a2.CorrectAnswer'

데모 : http://sqlize.com/8Wjawg6g4N

관련 문제