我正在与外键链接的3个表上工作,也设置为ON UPDATE CASCADE
和ON DELETE CASCADE
。这些表是category
, subcategory
和products
。
products
表- PID(PK), productname, subcatid(FK)
subcategory
table - subcatid(PK), subcategoryname, catid(FK)
category
表- catid(PK), categoryname
当我想更改产品名称时,对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();
}
}
?>
没有使用产品名作为外键,所以一个简单的标准
UPDATE products SET productname='New Name of Product' where PID=XXX;
就可以了。产品中的亚硝酸盐也是如此。只要新的子类别ID存在于子类别表中,就可以更改产品。还是通过一个简单的
UPDATE products SET subcatid=New_ID where PID=XXX;