简体   繁体   English

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

[英]It is not outputting any rows for my query result, why is this?

When I push the submit button to submit the form, there are no records being displayed in my query result. 当我按下提交按钮提交表单时,我的查询结果中没有显示任何记录。 I don't know why this is. 我不知道为什么会这样。 I know the query is correct as I have tested it before but when I have included the WHERE clause it doesn't work, but I am sure this is the right code when trying to retrieve rows depending on what is entered in the form. 我知道查询是正确的,因为我之前已经测试过,但是当我包含WHERE子句时它不起作用,但我确信这是在尝试根据表单中输入的内容检索行时的正确代码。

Also I am trying to display StudentAnswer in the array as AnswerContent but when I do this AnswerContent is only displayed for each AnswerId. 此外,我试图在数组中显示StudentAnswer作为AnswerContent,但是当我这样做时,AnswerContent仅显示每个AnswerId。 How do I do it to display it for each StudentAnswer?. 如何为每个StudentAnswer显示它? StudentAnswer is the same as AnswerId as whichever answer is selected it will retrieve the answer by its id and store it in the StudentAnswer field. StudentAnswer与AnswerId相同,因为无论选择哪个答案,它都会通过其ID检索答案并将其存储在StudentAnswer字段中。 Please help. 请帮忙。

Below is my code: 以下是我的代码:

<!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
  }
?>

Thank You :) 谢谢 :)

Your first LEFT JOIN is referencing an alias a2 which doesn't exist yet at that point, because it is joined later in the next LEFT JOIN : 你的第一个LEFT JOIN引用了一个别名a2 ,该别名在那时还不存在,因为它将在下一个LEFT JOIN

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

I'm guessing that reference to a2 is supposed to actually be a . 我猜测对a2引用实际上应该是a Note that if you had error reporting turned on in your development envrionment ( error_reporting(E_ALL) at the top of the page), you would easily catch this because you would get the following error message when trying to run the query: 请注意,如果您在开发环境中打开了错误报告(页面顶部的error_reporting(E_ALL) ),则很容易捕获此信息,因为在尝试运行查询时会出现以下错误消息:

Unknown column 'a2.CorrectAnswer' in 'on clause' 'on子句'中的未知列'a2.CorrectAnswer'

Demonstration: http://sqlize.com/8Wjawg6g4N 演示: http//sqlize.com/8Wjawg6g4N

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM