HTML表格无法正确显示MySQL数据

时间:2019-03-03 12:24:56

标签: php html mysql sql html-table

我运行多个MySQL查询,并基于变量(例如人员及其功能等)。它应该将数据输出到HTML表中的行和列中。但是,在运行代码时,最终会陷入一团糟,数据没有跟随正确的列。有关更多信息,请参见下图(https://imgur.com/alRkog8)。我似乎无法弄清楚自己在做什么错,因为在我看来我已经正确放置了所有的“ tr”和“ td”。This is how the HTML table ends up looking on the webpage       

    session_start();
    if(!isset($_SESSION['user1'])){
        header("location:".WEBROOT."index.php");
        exit();
    }
include_once DOCROOT . 'DBconnect.php';
    ?>

<html>
<head>
    <?php
    $title = "Participants";
    $file = __FILE__;
    include DOCROOT . 'pages/menu.php';
    ?>
<div id="content">
<div class="widecontentitem" id="lastitem">
    <?php
    $result = mysqli_query($db_link, 'SELECT institutionID FROM users WHERE users.UserID = \'' . $_SESSION['user1'] . '\'');
    if (!$result) {
        die('Invalid query: ' . mysqli_error());
    }
    $institutionID = "";
    while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
        $institutionID = $row[0];
    }
    print "<input type=\"hidden\" id=\"institution\" value=\"" . $institutionID . "\"> ";
    $result = mysqli_query($db_link, 'SELECT participants.name, participants.surname, participants.email, participants.special, participants.housingcomments, participants.functionID, participants.infix, participants.committee, participants.dateOfBirth, participants.housing, participants.countryID, participants.participantID FROM participants WHERE participants.institutionID =' . $institutionID);
    if (!$result) {
        die('Invalid query:2 ' . mysqli_error());
    }
    print "<table>";
    print "<tr><th><b>Function</b></th><th><b>Name</b></th><th><b>Surname</b></th><th><b>Date Of Birth</b></th><th><b>Country</b></th><th><b>Committee</b></th></tr>";
    while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
       if($row[9]==1){
          $row[0] = "(H)" . $row[0];
        }

        if( $row[5]==4){
            $title = "Delegate ";
            $result3 = mysqli_query($db_link, 'SELECT delegation FROM delegations WHERE ambassadorID =' . $row[11] );
            $num_rows = mysqli_num_rows($result3);
            if($num_rows==1){
                $title = "Ambassador ";
            }
            $result2 = mysqli_query($db_link, 'SELECT delegation FROM delegations WHERE delegationID = ' . $row[10] );
            while ($row2 = mysqli_fetch_array($result2, MYSQLI_NUM)) {
                print "<tr><td>$title </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td>$row2[0] </td><td>$row[7]   </td></tr>";
            }
        }
        if( $row[5]==2){
            print "<tr><td>MUN-Director </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td></td></tr>";
        }
        if($row[5]==5){
            print "<tr><td>President </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td>$row[7] </td></tr>";
        }
        if($row[5]==6){
            print "<tr><td>Deputy President </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td>$row[7]  </td></tr>";
        }
        if($row[5]==7){
            print "<tr><td>Chair </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td>$row[7] </td></tr>";
        }
        if($row[5]==8){
            print "<tr><td>Deputy Chair </td><td>$row[0] </td><td>$row[1] </td><td>$row[8] </td><td></td><td>$row[7] </td></tr>";
        }
        if($row[4]!=NULL || $row[3]!=NULL){
            print "<table>";
            if($row[3]!=NULL){
                print "<tr><td colspan=\"6\">Comments: </td><td>$row[3] </td></tr>";
            }
            if($row[4]!=NULL){
                print "<tr><td colspan=\"6\">Housing: </td><td>$row[4] </td></tr>";
            }
            print "</table>";
        }
    }
    print "</table>";
    ?>
</div>
<?php include '../footer.php';?>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

至少一个问题可能是从此处嵌套

元素:

if($row[4]!=NULL || $row[3]!=NULL){
            print "<table>";
            if($row[3]!=NULL){

来自MDN Table Element Doc

  

允许的内容按以下顺序:

     
      
  • 可选的<caption>元素
  •   
  • 零个或多个<colgroup>元素,
  •   
  • 可选的<thead>元素
  •   
  • 以下任一项:      
        
    • 零个或多个<tbody>元素
    •   
    • 一个或多个<tr>元素
    •   
  •   
  • 可选的<tfoot>元素
  •   

您可能想使用类似Nu Html的方法来验证HTML。一种使用方式是

  • 右键单击页面,然后选择“查看页面源”或类似内容,具体取决于您的浏览器
  • 全选并复制
  • 在验证器中选择text input进行校验
  • 粘贴页面源,然后单击“检查”