它没有为我的查询结果输出任何行,为什么会这样?

时间:2011-10-20 18:30:35

标签: php mysql database arrays

当我按下提交按钮提交表单时,我的查询结果中没有显示任何记录。我不知道为什么会这样。我知道查询是正确的,因为我之前已经测试过,但是当我包含WHERE子句时它不起作用,但我确信这是在尝试根据表单中输入的内容检索行时的正确代码。 / p>

此外,我正在尝试将数组中的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 个答案:

答案 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))中启用了错误报告,则可以轻松捕获此信息,因为在尝试运行查询时会出现以下错误消息:

  

'on clause'

中的未知列'a2.CorrectAnswer'

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