PHP更新查询未执行,弹出窗体关闭

时间:2018-01-24 13:28:02

标签: javascript php html mysql

我正在为学校创建一个网上商店。编辑存储在我的mysql数据库中的现有产品时出现问题。当我点击“编辑产品”按钮时,它只会关闭我创建的弹出窗体。

我一直试图解决这个问题一段时间,但我似乎无法解决它。

我收到此错误: 注意:未定义的索引:productcode

这是本帖子底部的第一行更新产品。

edit page picture

edit page popup form

编辑页面:

<?php
include 'includes/header.php';
include 'clsproducts.php';

$clsproducts = new clsproducts();

if (session_id() == '' || !isset($_SESSION)) {
    session_start();
}

if (!isset($_SESSION["uuid"])) {
    header("location:inloggen.php");
}

if ($_SESSION["rol"] != "admin") {
    header("location:index.php");
}

$page = "product-toevoegen";
?>
<!DOCTYPE html>
<html lang="en">

<body>
<!-- Navigation -->
<?php
include 'includes/navigation.php';
?>
<!-- content -->
<table class="table table-hover table-edit">
    <thead>
    <tr>
        <th scope="col">Afbeelding</th>
        <th scope="col">Product code</th>
        <th scope="col">Product naam</th>
        <th scope="col">Product beschrijving</th>
        <th scope="col">Product vooraad</th>
        <th scope="col">Prijs</th>
        <th scope="col">categorie</th>
        <th scope="col">update</th>
    </tr>
    </thead>
    <tbody>
<?php $clsproducts->showproducts();?>

    </tbody>
</table>

<!-- Footer -->
<?php
include 'includes/footer.php';
if (isset($_POST['edit'])) {

    $clsproducts->editproducts();
}

if (isset($_POST['edit-product'])) {

    $clsproducts->update();
}
?>
<script>
    $(document).ready(function(){
        $("#close").click(function(){
            $("#editpopup").hide();
        });

    });
</script>

</body>

</html>

展示产品:

public static function showproducts()
{
    $result = $GLOBALS['conn']->prepare("SELECT * FROM products ORDER BY id ASC ");
    $result->execute();

    $obj = $result->fetchAll(PDO::FETCH_OBJ);


    foreach ($obj as $item) {
        echo '<tr>';
        echo '<td style="vertical-align: middle"><img  style="height: 80px;width: 80px; "src="images/' . $item->product_img . '"/></td>';
        echo '<td style="vertical-align: middle">' . $item->product_code . '</td>';
        echo '<td style="vertical-align: middle">' . $item->product_naam . '</td>';
        echo '<td style="vertical-align: middle">' . $item->product_beschrijving . '</td>';
        echo '<td style="vertical-align: middle">' . $item->product_vooraad . '</td>';
        echo '<td style="vertical-align: middle">' . $item->prijs . '</td>';
        echo '<td style="vertical-align: middle">' . $item->categorie . '</td>';
        echo '<td><form method="POST" action="" id="needs-validation" style="width: 70px;">
                <input type="hidden" name="productcode" value="' . $item->product_code . '">
                <button name="edit" type="submit" style="width: 70px; margin-bottom: 10px;" class="btn btn-outline-primary">Edit</button>
                <button name="delete" type="submit" style="width: 70px;" class="btn btn-outline-danger">Delete</button>
                </form>';
        echo '</tr>';
    }
}

编辑产品:

public static function editproducts()
{
        $product_code = $_POST["productcode"];
        $result = $GLOBALS['conn']->prepare("SELECT * from products WHERE product_code = '$product_code' ");
        $result->execute();
        $obj = $result->fetchAll(PDO::FETCH_OBJ);
        $obj = $obj[0];

        echo '<div id="editpopup" class="modal">';

        echo '<div class="modal-content">';
        echo '<div class="modal-header">';
        echo '<h2>Bewerk ' . $obj->product_naam . '</h2>';
        echo '<span id="close" class="close">&times;</span>';
        echo '</div>';
        echo '<div class="modal-body">';
        echo '<form method="POST" action="" id="needs-validation" novalidate>';
        echo '<fieldset disabled>';
        echo '<div class="form-group row">';
        echo '<label for="inputproductcode">Product code</label>';
        echo '<input type="text" id="inputproductcode" class="form-control" value="' . $obj->product_code . '" name="productcode" required>';
        echo '</div>';
        echo '</fieldset>';
        echo '<div class="form-group row">';
        echo '<label for="inputproductnaam">Product naam</label>';
        echo '<input type="text" class="form-control" id="inputproductnaam" value="' . $obj->product_naam . '" name="product_naam" required>';
        echo '<div class="invalid-feedback">';
        echo 'vul een product naam in.';
        echo '</div>';
        echo '</div>';
        echo '<div class="form-group row">';
        echo '<label for="inputproductbeschrijving">Product beschrijving</label>';
        echo '<input type="text" class="form-control" id="inputproductbeschrijving" name="product_beschrijving" value="' . $obj->product_beschrijving . '" required>';
        echo '<div class="invalid-feedback">';
        echo 'vul een product beschrijving in.';
        echo '</div>';
        echo '</div>';
        echo '<div class="form-group row">';
        echo '<label for="inputvooraad">Vooraad product</label>';
        echo '<input type="number" class="form-control" id="inputvooraad" name="product_vooraad" value="' . $obj->product_vooraad . '" required>';
        echo '<div class="invalid-feedback">';
        echo 'vul een vooraad in.';
        echo '</div>';
        echo '</div>';
        echo '<div class="form-group row">';
        echo '<label for="inputprijs">Prijs</label>';
        echo '<input type="number" step="any" class="form-control" id="inputprijs" name="prijs" value="' . $obj->prijs . '" required>';
        echo '<div class="invalid-feedback">';
        echo 'Vul een prijs in.';
        echo '</div>';
        echo '</div>';
        echo '<div class="form-group row">';
        echo '<label for="inputcategorie">categorie</label>';
        self::categorie();
        echo '</div>';


        echo '<div class="form-group row">';
        echo '<button name="edit-product" type="submit" class="btn btn-primary">Wijzig Product</button>';
        echo '</div>';
        echo '</form>';
        echo '</div>';
        echo '<div class="modal-footer">';
        echo '<h3>test</h3>';
        echo '</div>';
        echo '</div>';

        echo '</div>';
}

更新产品:

public static function update()
{
        $product_code = $_POST["productcode"];
        $product_naam = $_POST["product_naam"];
        $product_beschrijving = $_POST["product_beschrijving"];
        $product_vooraad = $_POST["product_vooraad"];
        $prijs = $_POST["prijs"];
        $categorie = $_POST["categorie"];
        $result = $GLOBALS['conn']->prepare("UPDATE products SET product_naam = '$product_naam', product_beschrijving = '$product_beschrijving', product_vooraad = '$product_vooraad', prijs = '$prijs', categorie = '$categorie' WHERE product_code = '$product_code'");
        $result->execute();
}

1 个答案:

答案 0 :(得分:0)

跟进评论,在HTML5中(默认情况下,如果您不定义您正在使用的HTML版本),带有提交类型的按钮标记将始终提交表单,重新加载页面。如果您根本没有设置类型属性,则默认为“提交”类型。

如果您不想进行表单操作,请尝试在按钮上设置type="button"