道歉的标题道歉。 我目前有一个编辑表单,其中可以选择产品,详细信息显示在可以编辑的表单中。不幸的是,编辑产品表中的每个产品时都会编辑,而不仅仅是选择的产品。要选择我正在使用的产品名称= productnameinput等等(下面的代码)
数据库:http://prnt.sc/f4tf5g(编辑前)
尝试在更新查询的末尾添加以下WHERE语句:
// edit product in database
$query="
SELECT * FROM category
";
$result = $DBH->prepare($query);
$result->execute();
$categories = $result->fetchAll();
//we need to select all products frist
$query3 = "
SELECT * FROM product
";
$result3 = $DBH->prepare($query3);
$result3->execute();
$allProducts = $result3->fetchAll();
//When the Product is selected this function is run
if (isset($_POST['choose'])) {
$query2 = "
SELECT product.*, category.* FROM product LEFT JOIN category ON category.CategoryID = product.CategoryID WHERE product.ProductName = :prodname
";
$result2 = $DBH->prepare($query2);
$result2->bindParam(':prodname', $_POST['product_name']);
$result2->execute();
$product = $result2->fetch();
}
//When the Update button is Pressed
if (isset($_POST['update'])) {
$query4 = "
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE product.ProductName = :prodname
";
$result4 = $DBH->prepare($query4);
$result4->bindParam(':newCatId', $_POST['newcategory']);
$result4->bindParam(':newProdName', $_POST['productName']);
$result4->bindParam(':newProdDesc', $_POST['productDescription']);
$result4->bindParam(':newStock', $_POST['stockCount']);
$result4->bindParam(':prodname', $_POST['product_name']);
$result4->execute();
}
PHP:
Pack
答案 0 :(得分:-1)
您正在更新整个表格,更新中没有过滤器
$query4 = "
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock
";