每次刷新表单时都会添加额外的记录

时间:2014-09-06 22:32:24

标签: php mysql mysqli

我用来通过php将产品信息插入mysql的代码就是这个。

<?php include 'dbconnect.php'; ?>

<head>
</head>

<body>
    <form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
    <input type="text" name="product_name"/>
    <input type="text" name="product_price" />

        <select name="product_cat">
        <?php
        $results = $connect->query("SELECT * FROM categories ORDER BY cat_name");
            while($row = $results->fetch_array()) {
            extract($row);
            echo "<option value='"."{$cat_id}"."'>"."{$cat_name}"."</option>";
            }  
        $results->free();
        ?>
        </select>

    <input type="submit" name="submit" value="Submit">
    </form>

    <?php   
    if($_POST){
    $sql = "INSERT INTO products (prd_name, prd_price) VALUES(?,?)";
    if($stmt = $connect->prepare($sql) )
        {
            $productname = $_POST['product_name'];
            $productprice = $_POST['product_price'];
            $categoryid = $_POST['product_cat'];
            $stmt->bind_param("si", $productname, $productprice);
        if($stmt->execute())
            {
                $lastid = $stmt->insert_id;
                $productsku = "PR".$productlastid;

                $insert_row = $connect->query("INSERT INTO categoryproducts (CategoryId, productId) VALUES ($categoryid, $lastid)");
                $insert_row = $connect->query("UPDATE products SET productSku = '".$productsku."' WHERE productId =".$productlastid."");

                echo "Created";
                $stmt->close();
            }else{
                    die("Unable to create category.");
                }
                }else{
                die("Unable to prepare statement.");
            }
                $connect->close();
            }
        ?>    

</body>
</html>

代码工作正常,正如我希望的那样,但每次保存记录后我刷新表单时,会在表categoryproducts中创建一条额外的记录。我最好的猜测是,在执行语句之后,问题出现在这里

if($stmt->execute())
        {
            $lastid = $stmt->insert_id;
            $productsku = "PR".$productlastid;

            $insert_row = $connect->query("INSERT INTO categoryproducts (CategoryId, productId) VALUES ($categoryid, $lastid)");
            $insert_row = $connect->query("UPDATE products SET productSku = '".$productsku."' WHERE productId =".$productlastid."");

            echo "Created";
            $stmt->close();
        }else{
                die("Unable to create category.");
            }
            }else{
            die("Unable to prepare statement.");
        }
            $connect->close();
        }

我想我需要在提交表单后在某处关闭连接。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

而不是if($_POST){使用if(isset($_POST['submit'])){将标题重定向到同一页面或您选择的其他页面。

即:并且使用ob_start();将阻止获得 Cannot modify header information - headers already sent by...

<?php 
ob_start();
?>

<?php include 'dbconnect.php'; ?>

// ... 

<?php 

if(isset($_POST['submit'])){
    $sql = "INSERT INTO products (prd_name, prd_price) VALUES(?,?)";
    if($stmt = $connect->prepare($sql) )
        {
            $productname = $_POST['product_name'];
            $productprice = $_POST['product_price'];
            $categoryid = $_POST['product_cat'];
            $stmt->bind_param("si", $productname, $productprice);
        if($stmt->execute())
            {
                $lastid = $stmt->insert_id;
                $productsku = "PR".$productlastid;

                $insert_row = $connect->query("INSERT INTO categoryproducts (CategoryId, productId) VALUES ($categoryid, $lastid)");
                $insert_row = $connect->query("UPDATE products SET productSku = '".$productsku."' WHERE productId =".$productlastid."");

                // echo "Created";

                $stmt->close();

                header("Location: this_page.php");
                exit;

            }else{
                    die("Unable to create category.");
                }
                }else{
                die("Unable to prepare statement.");
            }
                $connect->close();
            } // brace for if(isset($_POST['submit']))
?>

您还可以考虑使用Ajax,或使用两个不同的文件,一个用于表单,另一个用于带有标头重定向的PHP / SQL。