如果最后一次插入失败,如何确保使用旧值重新填充表单?

My code is:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Insert Item - SomuFinance</title>
    <link rel="stylesheet" type="text/css" href="indexStyle.css">
    <script type="text/javascript" src="scripts/jquery-3.1.0.min.js"></script>
</head>
<body>
    <div id="addItemContainer">
        <h1>Insert Item</h1>
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
            <div class="leftAligned">
                <?php
                    $save_vals = FALSE;
                    if(!empty($_POST['submit']))
                    {
                        $shop = $_POST['shop'];
                        $category = $_POST['category'];
                        $item = $_POST['item'];
                        $qnty = $_POST['qnty'];
                        $unit = $_POST['unit'];
                        $price_based_on = $_POST['price_based_on'];
                        $mrp = $_POST['mrp'];
                        $sellers_price = $_POST['sellers_price'];
                        $last_updated_on = $_POST['last_updated_on'];
                        $save_vals = $_POST['save_vals'];
                    }
                ?>
                <div class="inp">
                    <label for="shop">Shop : </label>
                    <input type="text" id="shop" name="shop" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $shop;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="category">Category : </label>
                    <input type="text" id="category" name="category" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $category;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="item">Item : </label>
                    <input type="text" id="item" name="item" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $item;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="qnty">Quantity : </label>
                    <input type="text" id="qnty" name="qnty" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $qnty;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="unit">Unit : </label>
                    <input type="text" id="unit" name="unit" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $unit;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="price_based_on">Price based on : </label>
                    <select name="price_based_on" id="price_based_on">
                        <option value="kilos" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='kilos'){echo 'selected';}} ?>>Kilos</option>
                        <option value="packet" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='packet'){echo 'selected';}} ?>>Packet</option>
                        <option value="bottle" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='bottle'){echo 'selected';}} ?>>Bottle</option>
                        <option value="box" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='box'){echo 'selected';}} ?>>Box</option>
                        <option value="piece" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='piece'){echo 'selected';}} ?>>Piece</option>
                    </select>
                </div> <br>
                <div class="inp">
                    <label for="mrp">MRP (₹) : </label>
                    <input type="text" id="mrp" name="mrp" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $mrp;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="sellers_price">Seller's Price (₹) : </label>
                    <input type="text" id="sellers_price" name="sellers_price" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $sellers_price;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="last_updated_on">Last Updated on : </label>
                    <input type="date" id="last_updated_on" name="last_updated_on" value="<?php date_default_timezone_set('Asia/Kolkata'); if((!empty($_POST['submit']))&&($save_vals)){echo $last_updated_on;} else echo date("Y-m-d") ?>">
                </div>
            </div>
            <div class="inp">
                <input id="insertButton" type="submit" name="submit" value="Insert">
            </div>
            <div id="message">
                <?php
                    if(isset($_POST['submit']))
                    {
                        $shop = $_POST['shop'];
                        $category = $_POST['category'];
                        $item = $_POST['item'];
                        $qnty = $_POST['qnty'];
                        $unit = $_POST['unit'];
                        $price_based_on = $_POST['price_based_on'];
                        $mrp = $_POST['mrp'];
                        $sellers_price = $_POST['sellers_price'];
                        $last_updated_on = $_POST['last_updated_on'];
                        $result=null;

                        $dbc =  mysqli_connect('localhost','root','atlantis2016','itemDB')
                                    or die("Error Connecting to Database");

                        $query = "INSERT INTO grocery VALUES ('0', '$shop', '$category', '$item', '$qnty', '$unit', '$price_based_on', '$mrp', '$sellers_price', '$last_updated_on')";

                        if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
                        {
                            $result = mysqli_query($dbc, $query)
                                                or die(mysqli_error($dbc));
                        }

                        if($result)
                        {
                            echo '<span class="success">Item Inserted Successfully!</span>';
                            $_POST['save_vals']=FALSE;  
                        }
                        else
                        {
                            echo '<span class="failure">Failed to insert Item.</span>';
                            $_POST['save_vals']=TRUE;
                        }
                    }
                ?>
                <script>
                $(document).ready(function(){
                    $("#message").fadeIn(400);
                });
                </script>
            </div>
        </form>
    </div>
</body>
</html>

I'm trying to determine whether to load the previously posted values if the operation was unsuccessful - otherwise not. If i were to simply set the value of $save_vals at the end of the script, they wouldn't exist at the beginning the next time the page is loaded. So I tried manually setting $_POST['save_vals'] so that it is available the next time the page is loaded. But I get the error:

Notice: Undefined index: save_vals in E:\wamp\www\SomuFinance\insertItem.php on line 27

Line 27 is : $save_vals = $_POST['save_vals']; What am I doing wrong? How do I ensure that the previous values are loaded ONLY IF the insertion was unsuccessful?

EDIT : While the use of a hidden input type solves the undefined problem, how do I ensure that the form is refilled ONLY when the insertion failed the last time?

Here's my updated code after using an input of hidden type:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Insert Item - SomuFinance</title>
    <link rel="stylesheet" type="text/css" href="indexStyle.css">
    <script type="text/javascript" src="scripts/jquery-3.1.0.min.js"></script>
</head>
<body>
    <div id="addItemContainer">
        <h1>Insert Item</h1>
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
            <div class="leftAligned">
                <?php
                    $save_vals = FALSE;
                    if(!empty($_POST['submit']))
                    {
                        $shop = $_POST['shop'];
                        $category = $_POST['category'];
                        $item = $_POST['item'];
                        $qnty = $_POST['qnty'];
                        $unit = $_POST['unit'];
                        $price_based_on = $_POST['price_based_on'];
                        $mrp = $_POST['mrp'];
                        $sellers_price = $_POST['sellers_price'];
                        $last_updated_on = $_POST['last_updated_on'];
                        $save_vals = $_POST['save_vals'];
                    }
                ?>
                <div class="inp">
                    <label for="shop">Shop : </label>
                    <input type="text" id="shop" name="shop" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $shop;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="category">Category : </label>
                    <input type="text" id="category" name="category" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $category;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="item">Item : </label>
                    <input type="text" id="item" name="item" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $item;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="qnty">Quantity : </label>
                    <input type="text" id="qnty" name="qnty" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $qnty;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="unit">Unit : </label>
                    <input type="text" id="unit" name="unit" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $unit;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="price_based_on">Price based on : </label>
                    <select name="price_based_on" id="price_based_on">
                        <option value="kilos" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='kilos'){echo 'selected';}} ?>>Kilos</option>
                        <option value="packet" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='packet'){echo 'selected';}} ?>>Packet</option>
                        <option value="bottle" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='bottle'){echo 'selected';}} ?>>Bottle</option>
                        <option value="box" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='box'){echo 'selected';}} ?>>Box</option>
                        <option value="piece" <?php if((!empty($_POST['submit']))&&($save_vals)){if($price_based_on=='piece'){echo 'selected';}} ?>>Piece</option>
                    </select>
                </div> <br>
                <div class="inp">
                    <label for="mrp">MRP (₹) : </label>
                    <input type="text" id="mrp" name="mrp" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $mrp;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="sellers_price">Seller's Price (₹) : </label>
                    <input type="text" id="sellers_price" name="sellers_price" value="<?php if((!empty($_POST['submit']))&&($save_vals)){echo $sellers_price;} ?>">
                </div> <br>
                <div class="inp">
                    <label for="last_updated_on">Last Updated on : </label>
                    <input type="date" id="last_updated_on" name="last_updated_on" value="<?php date_default_timezone_set('Asia/Kolkata'); if((!empty($_POST['submit']))&&($save_vals)){echo $last_updated_on;} else echo date("Y-m-d") ?>">
                </div>
                <input type="hidden" id="save_vals" name="save_vals">
            </div>
            <div class="inp">
                <input id="insertButton" type="submit" name="submit" value="Insert">
            </div>
            <div id="message">
                <?php
                    if(isset($_POST['submit']))
                    {
                        $shop = $_POST['shop'];
                        $category = $_POST['category'];
                        $item = $_POST['item'];
                        $qnty = $_POST['qnty'];
                        $unit = $_POST['unit'];
                        $price_based_on = $_POST['price_based_on'];
                        $mrp = $_POST['mrp'];
                        $sellers_price = $_POST['sellers_price'];
                        $last_updated_on = $_POST['last_updated_on'];
                        $result=null;

                        $dbc =  mysqli_connect('localhost','root','atlantis2016','itemDB')
                                    or die("Error Connecting to Database");

                        $query = "INSERT INTO grocery VALUES ('0', '$shop', '$category', '$item', '$qnty', '$unit', '$price_based_on', '$mrp', '$sellers_price', '$last_updated_on')";

                        if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
                        {
                            $result = mysqli_query($dbc, $query)
                                                or die(mysqli_error($dbc));
                        }

                        if($result)
                        {
                            echo '<span class="success">Item Inserted Successfully!</span>';
                            $_POST['save_vals']=FALSE;  
                        }
                        else
                        {
                            echo '<span class="failure">Failed to insert Item.</span>';
                            $_POST['save_vals']=TRUE;
                        }
                    }
                ?>
                <script>
                $(document).ready(function(){
                    $("#message").fadeIn(400);
                });
                </script>
            </div>
        </form>
    </div>
</body>
</html>

$_POST isn't persistent between loads, the same as $save_vals, it only contains elements submitted by the form. So you would either need to add an input to the form with <input type="hidden" name="save_vals" value="<?php echo $save_vars; ?>"> or use session variables e.g. $_SESSION['save_vars']