模糊標題的道歉。 我目前有一個編輯表單,可以在其中選擇產品,並以可編輯的形式顯示細節。不幸的是,編輯產品表中的每個產品時,都會編輯而不是僅選擇產品。要選擇我使用的產品,其中產品名稱= productnameinput等等等等(代碼如下所示)查詢更新所有記錄而不是與輸入相匹配的記錄?
數據庫:http://prnt.sc/f4tf5g(編輯之前)
嘗試添加以下語句的WHERE在更新查詢的末尾:
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE product.ProductName = :prodname
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE ProductName = :prodname
PHP:
// 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();
}
你需要一個'where'在'update'條款。 – chris85
現在就試試,謝謝。將更新問題,如果不成功,但我認爲你是對的 – Xander
@ chris85很確定你是對的,但我仍然收到錯誤,我已更新我的問題。我應該包括$ post產品名稱等嗎? – Xander