【问题标题】:display data from the MySQL database into an HTML将 MySQL 数据库中的数据显示为 HTML
【发布时间】:2021-02-08 04:19:28
【问题描述】:

我尝试使用 PHP 将 MySQL 数据库中的数据显示到 HTML 表格中,一切正常,除了“请求的汽车”它显示在表格上方而不是表格内部

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>

<style>
table, th, td {
    border: 1px solid black;
}
</style>
    <!-- <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
    <title>tables</title>
</head>
<body>
    <table>
    
    <tr>
    <th>Customer name</th>
    <th>Customer phone</th>
    <th>Age</th>
    <th>Cars requested</th>
    <?php
    $servername = "localhost";
    $username = "root"; // For MYSQL the predifined username is root
    $password = ""; // For MYSQL the predifined password is " "(blank)
    $db = "car";
    
// Create connection
$conn = new mysqli($servername, $username, $password,$db);
if ($conn->connect_error)  {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT fname, phone, age, test from info";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["fname"]. "</td> <td>" . $row["phone"] . "</td><td>" . $row["age"] . "</td></td>" .$row["test"] . "</td></tr>";
    }
    echo "</table>";
}

else
{
    echo "0 result";
}
$conn->close();
?>
    </tr>
    </table>
</body>
</html>

我该如何解决这个错误?

【问题讨论】:

  • 请求的汽车是一个复选框
  • .$row["test"] 之前的 "" 应该是 ""

标签: php html css mysql


【解决方案1】:

打印代码echo 应如下所示:

echo "<tr><td>" . $row["fname"]. "</td> <td>" . $row["phone"] . "</td><td>" . 
$row["age"] . "</td><td>" .$row["test"] . "</td></tr>";

表结构没有关闭。

【讨论】:

    【解决方案2】:
     "</td></td>" .$row["test"] 
    

    这是我猜的问题。应该是&lt;td&gt;&lt;/td&gt; 而不是&lt;/td&gt;&lt;/td&gt;

    【讨论】:

      猜你喜欢
      • 2012-03-03
      • 2014-04-22
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      相关资源
      最近更新 更多