html 代码中 sql 查询的第一行除外,未应用样式

Style is not being applied except for the first row from sql query in html code

我正在通过查询从 sql 数据库中检索所有行,并且我正在使用一个循环来获取所有行 我有一个 style.css 文件,我正在为 指定样式,但是,只有第一行接收样式并且样式没有应用于其余行。

<!DOCTYPE html>
<html>
<link href="styles.css" rel="stylesheet">
<head>
    <title>IIACSS search center</title>
</head>
<body>

<form method="post">
<label>Search</label>
<input type="text" name="search">
<input type="submit" name="submit">
</form>

<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");

$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
  <tr>
    <th>Project</th>
    <th>Question</th>
    <th>Sample</th>
  </tr>
  <?php
  for ($a=0;$row = $sth->fetch();$a++) {
  ?>
        <tr>
            <td><?php echo $row->Proj_Name; ?></td>
            <td><?php echo $row->Question;?></td>
            <td><?php echo $row->sample;?></td>
            <br><br>
        </tr>
    </table>

    <?php
  }
  }
  ?>
body {
  background-color: white;
}

h1 {
  color: black;

}

td {
  color: black;
  font-family: sans-serif;
}
th {
  color: blue;
  font-family: sans-serif;
}

这不是 CSS 问题,这是因为您在循环中关闭了 </table>,所以它在第一个 tr 之后结束了您的 table .

只需将其从循环中删除:

<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");

$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
  <tr>
    <th>Project</th>
    <th>Question</th>
    <th>Sample</th>
  </tr>
  <?php
  for ($a=0;$row = $sth->fetch();$a++) {
  ?>
        <tr>
            <td><?php echo $row->Proj_Name; ?></td>
            <td><?php echo $row->Question;?></td>
            <td><?php echo $row->sample;?></td>
            <br><br>
        </tr>

    <?php
  } ?>
      </table>
  <?php }
  ?>