php - 有人能解释一下为什么我的 MySQL 表数据没有显示吗?

标签 php mysql mysqli php-7.2

我是 PHP 和 MySQL 新手。我有一个 HTML 表和表单,可将​​输入的数据提交到 MySQL,但不会在 HTML 表中显示 MySQL 数据。这是供引用的图片:https://i.imgur.com/OEDd6Px.png 。如果可能的话,我希望提交的数据在提交时显示,但无法找到解决方案。提前致谢。

<?php
$host    = "localhost";
$user    = "root";
$pass    = "";
$db = "test";

$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) {
    die ('Failed to connect to MySQL: ' . mysqli_connect_error()); 
}

if(isset($_POST["asin"]))
{
    $asin =  $_POST["asin"];
    $category = $_POST["category"];
    $submit = "INSERT INTO `user_input`(`id`, `asin`, `category`, `date`) VALUES (NULL, '$asin', '$category', CURRENT_DATE())";

    $sql = mysqli_query($conn, $submit);
    if (!$sql) {
    die ('SQL Error: ' . mysqli_error($conn));
    }

    $display = "SELECT * FROM user_input";
    $result = mysqli_query($conn, $display);
    if (!$result) {
    die ('SQL Error: ' . mysqli_error($conn));
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>testEnv</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<style>
form {
    padding-bottom: 10px;
    padding-top: 10px;
}

table, thead, tbody, th, td {
    padding: 4px;
    border-collapse: collapse;
    border: 1px solid black;
}

form {
    font-size: 13px;
}

th, td {
    width: auto;
    text-align: center;
    font-size: 13px;
}
</style>
</head>
<body>
    <div class="container-fluid">
        <form id="form" method="post">
            <div>
            <label id="asinLabel" for="asin">ASIN:</label>
            <input id="asinInput" type="text" name="asin"></input>
            <label id="categoryLabel" for="category">Category:</label>
            <input id="categoryInput" type="text" name="category"></input>
            <input id="submit" type="submit" value="Submit"></input>
            </div>
        </form>
    </div>
    <div class="container-fluid">
        <table class="container-fluid">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>ASIN</th>
                    <th>Category</th>
                    <th>Date</th>
                    <th>6:00 AM</th>
                    <th>8:00 AM</th>
                    <th>10:00 AM</th>
                    <th>12:00 PM</th>
                </tr>
            </thead>
            <tbody id="tableBody">
                <?php
                while($row = mysqli_fetch_array($result));
                {  
                    echo '<tr>
                            <td>'.$row['id'].'</td>
                            <td>'.$row['asin'].'</td>
                            <td>'.$row['category'].'</td>
                            <td>'. date('m d, Y', strtotime($row['date'])) .'</td>
                            <td>'.$row['6am'].'</td>
                            <td>'.$row['8am'].'</td>
                            <td>'.$row['10am'].'</td>
                            <td>'.$row['12pm'].'</td>
                        </tr>';
                }
                mysqli_close($conn);
                ?>
            </tbody>
        </table>
    </div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script rel="script" type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script rel="script" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.js"></script>
<script rel="script" type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.js"></script>
</body>
</html>

最佳答案

尝试写

 $display = "SELECT * FROM user_input";
    $result = mysqli_query($conn, $display);
    if (!$result) {
    die ('SQL Error: ' . mysqli_error($conn));
}

挡住条件的一侧。

关于php - 有人能解释一下为什么我的 MySQL 表数据没有显示吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397284/

相关文章:

php - mysql从表中的两个不同列中选择两个不同的值

php - mysqli 忽略表中的第一行

php - 为什么这个 MySQL 选择查询不能与 WHERE 子句一起使用?

php - 查询运行次数与刷新次数相同

php - jQuery 和对象数组

php - 我可以使用 Laravel 中的 Controller 创建数据库表吗?

php - 从 mysql 中选择收藏夹并显示它们?

php - 更新我的数据库在 php 中不起作用

PHP MySQL计算AS不工作

Visual Studio Code 中的 PHP block 快捷方式