当插入数据存在时,PDO Mysql数据不显示

Problem when i displayed data from database i see all data in page. But when i'm about to put insert the data is unviewable.

enter image description here

this is the code:

    <?php
    include 'db.php';
    $stmt = $db->prepare('SELECT * FROM ' . $table);
    $stmt->execute();

    if(isset($_POST['btn-insert'])){

        $prodname = $_POST['prod_name'];
        $prodsupp = $_POST['prod_supplier'];
        $prodprice = $_POST['prod_price'];


    }
?>
<html>
    <head><title></title></head>
    <body>
            <table cellpadding="4" cellspacing="2" border='1'>
                <tr>
                    <th>Product ID</th>
                    <th>Product NAME</th>
                    <th>Product SUPPLIER</th>
                    <th>Product PRICE</th>
                </tr>
                <?php while($product = $stmt->fetch(PDO::FETCH_OBJ)) { ?>
                <tr>
                    <td><?php echo $product->prod_id; ?></td>
                    <td><?php echo $product->prod_name; ?></td>
                    <td><?php echo $product->prod_supplier; ?></td>
                    <td><?php echo $product->prod_price; ?></td>
                </tr>
                <?php } ?>
            </table>
            <br>
            <br>

            <form action="" method="post">
                <table cellspacing="2" cellpadding="2" border="1">
                    <tr>
                        <td>Product name:</td>
                        <td><input type="text" name="prod_name"></td>
                    </tr>
                    <tr>
                        <td>Product supplier:</td>
                        <td><input type="text" name="prod_supplier"></td>
                    </tr>
                    <tr>
                        <td>Product Price:</td>
                        <td><input type="text" name="prod_price"></td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" name="btn-insert" value="INSERT"></td>
                    </tr>
                </table>
            </form>

    </body>
</html>

and this is the code when i try to insert just change the php script on the top:

<?php
    include 'db.php';
    $stmt = $db->prepare('SELECT * FROM ' . $table);
    $stmt->execute();

    if(isset($_POST['btn-insert'])){

        $prodname = $_POST['prod_name'];
        $prodsupp = $_POST['prod_supplier'];
        $prodprice = $_POST['prod_price'];

        $db = new PDO('mysql:host' . $host . ';dbname=' . $dbname, $dbusername, $dbpassword);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $db->prepare("INSERT INTO tbl_product(prod_name, prod_supplier, prod_price) VALUES('$prodname', '$prodsupp', '$prodprice')");

    }
?>

this what happens: enter image description here

thats the problem i have encountered right at this moment.

You just created a conflict in your "stmt" variable... firstly

  $stmt = $db->prepare('SELECT * FROM ' . $table);
  $stmt->execute();

Then:

 $stmt = $db->prepare("INSERT INTO...

so since you prepared your insert statement,

This is not fetching anything.. check the conflict and it will be fine..

  $product = $stmt->fetch(PDO::FETCH_OBJ)

In the document, you first do a SELECT and then an INSERT (if $_POST data exists). But the select will not be able to show the INSERTed data before the page has been refreshed.

To show both existing and recently inserted data, you can do the following:

  1. Load current page with data (SELECT).
    1. If $_POST data exists:
    2. Sanitize and INSERT the $_POST data
  2. Reload the page after an INSERT.

You will now see existing and recently inserted data in the reloaded page.

Tip: To reload the page, you can use rowCount to check if a row was inserted:

if($stmt->rowCount() == 1) {
   header("Location: index.php");
} else {
    // Error
}