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

时间:2016-12-15 04:53:14

标签: php

我的代码是:

<!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>

如果操作不成功,我试图确定是否加载以前发布的值 - 否则不行。如果我只是在脚本末尾设置$ save_vals的值,那么在下次加载页面时它们就不会存在。所以我尝试手动设置$_POST['save_vals'],以便下次加载页面时可以使用它。但是我得到了错误:

  

注意:未定义的索引:第27行的E:\ wamp \ www \ SomuFinance \ insertItem.php中的save_vals

第27行是:$save_vals = $_POST['save_vals']; 我究竟做错了什么?如果插入不成功,我如何确保加载以前的值?

编辑:虽然使用隐藏输入类型可以解决未定义的问题,但如何确保仅在上次插入失败时重新填充表单?

使用隐藏类型输入后,我的更新代码为:

<!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>

1 个答案:

答案 0 :(得分:0)

$_POST在加载之间不是持久的,与$save_vals相同,它只包含表单提交的元素。因此,您需要使用<input type="hidden" name="save_vals" value="<?php echo $save_vars; ?>">向表单添加输入,或者使用会话变量,例如$_SESSION['save_vars']