首页 > 解决方案 > 通过带有列表框的模式更新但得到 - 警告:未定义的属性:PDOStatement::$product_category

问题描述

我正在尝试实现一个列表框以获取 Category 表中的值,但是在更新 Products 表时。

到目前为止,我有以下代码:

<?php
include ('connection.php');
$product_stmt = $dbh ->prepare("SELECT * FROM `products`");
   if ($product_stmt->execute() && $product_stmt->rowCount() > 0) {
   $product = $product_stmt->fetchObject(); ?>
<!--Modal to Edit Product-->
<div class="modal fade" id="edit_<?= $p['id']; ?>" >
    <div class="modal-dialog">
        <div class="modal-content">

            <!-- Modal Header -->
            <div class="modal-header">
                <h4 class="modal-title">Edit Product</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- Modal body -->
            <form method="post" action="edit_product.php?id=<?php echo $p['id']; ?>">
                <div class="modal-body">
                    <label for="name"><b>Product Name:&emsp;</b></label>
                    <input type="text" name="name" value="<?= $p["name"] ?>"/>
                    <br>
                    <label for="upc"><b>Product UPC:&emsp;</b></label>
                    <input type="text" name="upc" disabled value="<?= $p["upc"] ?>"/>
                    <br>
                    <label for="product_price"><b>Product Price:&emsp;</b></label>
                    <input type="number" min="0" step="any" name="product_price" value="<?= $p["product_price"] ?>"/>
                    <br>

                    <?php $category_edit_stmt = $dbh->prepare("SELECT * FROM `category` ORDER BY `category_name`");
                    if ($category_edit_stmt->execute() && $category_edit_stmt->rowCount() > 0) { ?>
                    <label for="product_category"><b>Product Category:&emsp;</b></label>
                    <select name="category_name" id="category_name">
                        <?php while ($row = $category_edit_stmt-> fetchObject()): ?>
                            <option value="<?=$row->category_name?>"><?= ($product_stmt->product_category == $row->category_name) ? "selected" : "" ?></option>
                        <?php endwhile;?>
                    </select>
                    <?php } ?>
                    <br>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span>Cancel</button>
                    <button type="submit" name="edit" class="btn btn-outline-primary btn-sm"><span class="glyphicon glyphicon-check"></span>Update</button>
                </div>
            </form>
            <?php } ?>
        </div>
    </div>
</div>

但它返回此警告:警告:未定义属性:第 35 行 C:\xampp\htdocs\Assignment_2\edit_delete_view_product_modal.php 中的 PDOStatement::$product_category

据我了解,这是因为它无法看到 product_category ,但我只有在搜索多个事物后才能找到解决方案。

我还有一个 edit_product.php,我在其中发布值以获取更新:

<?php
session_start();
include_once('connection.php');

if(isset($_POST['edit'])){
    try{
        //make use of prepared statement to prevent sql injection
        $category_edit_stmt = $dbh->prepare("UPDATE products SET `name` =:name, `product_price` =:product_price, `product_category` =:product_category ");
        $category_edit_stmt->execute(array(':name' => $_POST['name'] , ':upc' => $_POST['upc'] , ':product_price' => $_POST['product_price'], ':product_category' => $_POST['category_name'])) ;
        //message
        $_SESSION["success"]= 'Product Successfully Updated';
        header ("Location: test.php");

    }
    catch(PDOException $error){
        $_SESSION["error"]= 'Error Updating Product - Please enter valid values';
        header ("Location: test.php");
    }

}


?>

任何帮助将不胜感激。

干杯

标签: mysqlpdoupdating

解决方案


推荐阅读