mysql - 通过带有列表框的模式更新但得到 - 警告:未定义的属性: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">×</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: </b></label>
<input type="text" name="name" value="<?= $p["name"] ?>"/>
<br>
<label for="upc"><b>Product UPC: </b></label>
<input type="text" name="upc" disabled value="<?= $p["upc"] ?>"/>
<br>
<label for="product_price"><b>Product Price: </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: </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");
}
}
?>
任何帮助将不胜感激。
干杯
解决方案
推荐阅读
- excel - Microsoft Excel - Comparing 2 Column and Delete Duplicate by Row
- algorithm - How do i calculate the running time for a hybrid algorithm?
- apache-beam - Branching pipeline // fanout-fanin : Process each file in separate branch
- python - 如何将base64解码为单字节?
- r - 在 r 上递增的 for 循环
- html - Trouble Getting Div Elements Lined Up Correctly
- sql - JOIN 条件不能正常工作
- python - What to do when pyinstaller does not seem to be working?
- python - How to load object from pickle file without shadowing names from outer scope?
- swiftui - Navigation bar title stays inline in iOS 15