更新 MySQL 中包含外键列的表

发布于 2024-10-30 07:25:09 字数 2445 浏览 1 评论 0原文

我正在处理与外键链接的 3 个表,并且还设置为 ON UPDATE CASCADE 和 ON DELETE CASCADE 。这些表是categorysubcategoryproducts

产品表 - PID(PK)、产品名称、subcatid(FK)

子类别表 - subcatid(PK)、子类别名称、catid(FK)

类别表 - catid(PK)、categoryname

当我想要更改产品名称时,对 UPDATE products 表的正确查询是什么?另外,如果我想使用表单更改产品的子类别?

我使用的表单成功预填充了字段 - 产品名称、子类别名称,但它不会更新产品表中的记录,这意味着它什么也不做,也不会更改产品名称和子类别名称。

任何帮助表示赞赏。

我使用以下代码

<?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();
    }
}
?>

I am working on 3 tables linked with foreign keys and also set to ON UPDATE CASCADE and ON DELETE CASCADE. These tables are category, subcategory and products.

products table - PID(PK), productname, subcatid(FK)

subcategory table - subcatid(PK), subcategoryname, catid(FK)

category table - catid(PK), categoryname

What is the right query to UPDATE products table when I want to change the name of the product? Also if I want to change the subcategory of a product by using a form?

I am using a form which successfully prepopulates with the fields - product name, subcategory name but it does not update the records in products table, means it does nothing and does not changes the product name and subcategory name.

Any help is appreciated.

am using the following code

<?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();
    }
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余罪 2024-11-06 07:25:09

您没有使用产品名称作为外键,因此一个简单的标准

UPDATE products SET productname='New Name of Product' where PID=XXX;

就可以解决问题。产品中的 subcatid 也是如此。只要子类别表中存在新的子类别 ID,您就可以通过简单的操作将 products.subcatid 更改为您想要的任何内容

UPDATE products SET subcatid=New_ID where PID=XXX;

You're not using the product name as a foreign key, so a simple standard

UPDATE products SET productname='New Name of Product' where PID=XXX;

would do the trick. Same goes for the subcatid in products. As long as the new subcat ID exists in the subcategory table, you can change products.subcatid to whatever you want, again via a simple

UPDATE products SET subcatid=New_ID where PID=XXX;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文