Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我希望将数组中的数据库字段相互链接,然后显示信息_Php_Mysql_Database - Fatal编程技术网

Php 我希望将数组中的数据库字段相互链接,然后显示信息

Php 我希望将数组中的数据库字段相互链接,然后显示信息,php,mysql,database,Php,Mysql,Database,请仔细阅读,以便理解问题。这个问题是大学里的一项作业 有两个表,一个是答案表,另一个是学生答案表。我有6个感兴趣的领域,4个在回答表中,2个在学生回答表中。下表及其字段和数据 Answer Table: QuestionId AnswerId AnswerContent AnswerCorrect 1 1 Leeds 1 (true) 2 2

请仔细阅读,以便理解问题。这个问题是大学里的一项作业

有两个表,一个是答案表,另一个是学生答案表。我有6个感兴趣的领域,4个在回答表中,2个在学生回答表中。下表及其字段和数据

Answer Table:

  QuestionId     AnswerId    AnswerContent   AnswerCorrect
   1             1           Leeds                1 (true)
   2             2           Manchester           0 (false)
   3             3           Birmingham           0 (false)

StudentAnswer Table:

    StudentAnswer  QuestionId
        2                1
        3                1
        1                1
StudentAnswer字段包含答案ID,这些是学生答案,取决于他们为哪个问题选择的答案

现在,这些字段和其他字段存储在一个数组中,并显示在表中。PHP代码如下所示:

<table border='1'>
      <tr>
      <th>Session ID</th>
      <th>Question Number</th>
      <th>AnswerContent</th>
      <th>StudentAnswer</th>
      <th>Student ID</th>
      </tr>
      <?php

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

            echo "
      <tr>
      <td>{$row['SessionId']}</td>
      <td>{$row['QuestionNo']}</td>
      <td>{$row['AnswerContent']}</td>
      <td>{$row['StudentAnswer']} </td>
      <td>{$row['StudentId']}</td>
      </tr>";
        }
      ?>
    </table>
问题是StudentAnswer以int形式显示,我想以word形式显示,唯一的方法是链接到AnswerContent字段。问题是,如果我只使用$row['AnswerContent'],它会显示AnswerId的单词answer,这很好,但不会显示StudentAnswer。所以我的问题是,我怎样才能通过将学生答案与答案内容链接起来,以单词形式显示学生答案

我尝试了$row['StudentAnswerContent']=$row['StudentAnswer']=$row['AnswerContent'];但这完全适得其反。你能帮我弄明白这一点,并给我举个例子说明如何做到这一点吗

我想说,问题不在于质询。查询工作正常。我想让你看看$row['…']部分,告诉我如何将StudentAnswer和AnswerContent链接在一起,以便识别每个StudentAnswer的正确答案内容

下面显示了它当前从查询和数组中输出的内容:

Session ID  Question Number   AnswerContent     StudentAnswer    Student ID       
 ABB          1               Leeds                  1           u0867587   
 ABB          1               Leeds                  3           u1231231
以下是我真正想要输出的内容:

Session ID  Question Number   AnswerContent     StudentAnswer     Student ID       
     ABB          1           Leeds               Leeds            u0867587   
     ABB          1           Leeds               Birmingham       u1231231
第1行显示学生u0867587对问题1的答案所做的修改。正确答案是利兹,他把答案放在利兹。第2行显示学生U1231回答了相同的问题,显然正确答案仍然是利兹,但他选择了伯明翰

谢谢你,任何帮助都将不胜感激

包含查询解决方案的完整代码和提交以输出结果的表单:

<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";
SQL:

行:

说明:

第一次加入-获得学生答案

第二次加入-获得正确答案内容


第三次加入-获取学生答案内容

您的查询不太正确。您需要使用StudentAnswer再次连接到答案表以获取文本。像这样的东西应该可以

SELECT 
    SessionId,
    q.QuestionNumber,
    a.AnswerContent,
    a2.AnswerContent as StudentAnswerContent,
    StudentId
FROM Question q
    INNER JOIN StudentAnswer sa ON q.QuestionId = sa.QuestionId
    JOIN Answer a ON sa.QuestionId = a.QuestionId  
    JOIN Answer a2 ON sa.StudentAnswer = a2.AnswerId
WHERE
    (CorrectAnswer = '1')
ORDER BY $orderfield ASC";

可能是你昨天发布的这个问题的副本。不要发布副本。。。试着自己做作业。你没有让别人帮你做这件事,你在做我的标题Marc B。关于你的信息,我自己做了很多工作,这是一个很大的努力,因为我已经两年没有做php了。这甚至不是家庭作业,而是欧盟委员会的一个项目。如果你不喜欢我的问题,那就不要在我的页面上发表评论。没有人关心你愚蠢的评论。学着去做吧。停止滥用网站。它现在不输出任何记录。我想说的是,记录的输出取决于表单提交的细节。我已经编辑了问题的底部,以显示您给我的表单和查询。是否有任何问题,因为看它,似乎没有问题。正如您对联接的解释所述,那么我是否需要更改所有$rows['…']的数组,或者我是否可以保留它的原样?您可以保留它。使用mysql workbench或phpmyadmin尝试一个接一个地添加联接,并尝试找出哪个联接是错误的。我不知道是谁以负1否决了您的答案,但我将其放回0,因为有人尝试回答我的问题很好
SELECT 
    q.* ,
    sa.*,
    a.*,
    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)
ORDER BY $orderfield ASC";
$row['StudentAnswerContent']
SELECT 
    SessionId,
    q.QuestionNumber,
    a.AnswerContent,
    a2.AnswerContent as StudentAnswerContent,
    StudentId
FROM Question q
    INNER JOIN StudentAnswer sa ON q.QuestionId = sa.QuestionId
    JOIN Answer a ON sa.QuestionId = a.QuestionId  
    JOIN Answer a2 ON sa.StudentAnswer = a2.AnswerId
WHERE
    (CorrectAnswer = '1')
ORDER BY $orderfield ASC";