有人能解释为什么我的mysql表数据没有被显示吗?

ao218c7q  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(346)

我不熟悉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>
eaf3rand

eaf3rand1#

试着写

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

把情况排除在外。

相关问题