我正在處理與外鍵鏈接的3個表,並且也設置爲ON UPDATE CASCADE
和ON DELETE CASCADE
。這些表格是category
,subcategory
和products
。更新MySQL中包含外鍵列的表
products
表 - PID(PK),產品名稱,subcatid(FK)
subcategory
表 - subcatid(PK),subcategoryname,CATID(FK)
category
表 - CATID(PK),類別名
當我想更改產品的名稱時,對UPDATE
產品表的正確查詢是什麼?另外,如果我想通過使用表單來更改產品的子類別?
我正在使用一個成功預填充字段的表單 - 產品名稱,子類別名稱,但它不更新產品表中的記錄,意味着它什麼都不做,並且不會更改產品名稱和子類別名稱。
任何幫助表示讚賞。
我用下面的代碼
<?php
// Parse the form data and add inventory item to the system
if (isset($_POST['PRODUCT_NAME'])) {
$pid = mysql_real_escape_string($_POST['thisPID']);
$catalog_no = mysql_real_escape_string($_POST['CATALOG_NO']);
$product_name = mysql_real_escape_string($_POST['PRODUCT_NAME']);
$price = mysql_real_escape_string($_POST['PRICE']);
$composition = mysql_real_escape_string($_POST['COMPOSITION']);
$size = mysql_real_escape_string($_POST['SIZE']);
$subcat = mysql_real_escape_string($_POST['SUBCAT_ID']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("UPDATE products SET CATALOG_N0='$catalog_no', PRODUCT_NAME='$product_name', PRICE='$price', COMPOSITION='$composition', SIZE='$size', SUBCAT_ID='$subcat' WHERE PID='$pid'");
header("location: inventory_list.php");
exit();
}
?>
<?php
// Gather this product's full information for inserting automatically into the edit form below on page
if (isset($_GET['pid'])) {
$targetID = $_GET['pid'];
$sql = mysql_query("SELECT products.PID, products.CATALOG_NO, products.PRODUCT_NAME, products.PRICE, products.COMPOSITION, products.SIZE, products.SUBCAT_ID, subcategory.SUBCAT_ID, subcategory.SUBCATEGORY_NAME FROM (products, subcategory) WHERE subcategory.SUBCAT_ID=products.SUBCAT_ID AND PID='$targetID' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$catalog_no = $row["CATALOG_NO"];
$product_name = $row["PRODUCT_NAME"];
$price = $row["PRICE"];
$composition = $row["COMPOSITION"];
$size = $row["SIZE"];
$subcat = $row["SUBCAT_ID"];
}
} else {
echo "You dont have that product";
exit();
}
}
?>
什麼是你與你的表格試圖查詢?張貼它們。 – Treffynnon 2011-04-05 13:07:56
使用以下代碼 – henry 2011-04-05 13:24:37
使用代碼更新您的問題。評論不夠長。 – Treffynnon 2011-04-05 13:25:02