更新多个表:多对多关系

发布于 2024-12-23 16:43:11 字数 3650 浏览 0 评论 0原文

需要帮助更新表

我有来自要插入数据库的表单的数据数组。我有 5 个表

  • 产品
  • 过滤器
  • 产品过滤器
  • 加热器
  • 产品加热器

我能够将数据放入“产品”、“过滤器”和“产品”中。 “heater”表,但我不知道如何将数据放入“product_filter”和“product_filter”中“产品加热器”表。对任何教程的任何帮助或指导表示赞赏。

我的表格结构:

产品

  • id int(5)
  • 产品文本
  • 成本文本
  • 详细信息文本

过滤器

  • id int(5)
  • 过滤器文本
  • imgpath 文本

product_filter

  • id int (5)
  • id_product int(5)
  • id_filter int(5)

heater

  • id int(5)
  • 加热器文本
  • imgpath text

product_heater

  • id int(5)
  • id_product int(5)
  • id_heater int(5)

我想在

  • product 和 id_heater int(5) 之间建立多对多关系加热器
  • 产品加热器之间的多对多关系过滤器

这是代码:

PHP Syntax (Toggle Plain Text)
// Product data Update
$name = mysql_real_escape_string($_POST['product']);
$cost = mysql_real_escape_string($_POST['cost']);
$details = mysql_real_escape_string($_POST['details']);

$sql_title = "INSERT INTO product (
            id ,
            product ,
            cost ,
            details ,
            )
            VALUES (
            NULL , '$name' , '$cost' , '$details')";
if (!mysql_query($sql_title,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "records for product added<br />";         


// Filter update
// This is the array which is coming from the form
/*
    filtername
   Array ( [0] => ehiem 
            [1] => Hagan 
            [2] => Rena 
            [3] => jobo ) 

 filterimg
    Array ( [0] => img1.jpg 
            [1] => img2.jpg 
            [2] => img3.jpg 
            [3] => img4.jpg )
*/

$filtername = mysql_real_escape_string($filtername);
$filterimgpath = mysql_real_escape_string($filterimg);
$combined_array = array_combine($filtername, $filterimgpath);
$values = array();
foreach ($combined_array as $filtername => $filterimgpath)
{
    $values[] = "('$filtername', '$filterimgpath')";
}
$sql = "INSERT INTO filter (filter , imgpath) VALUES " . implode(', ', $values);
//echo $lastid = mysql_insert_id()."<br />";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "records added<br />";

//Product Filter Update table
    // This is where Im stuck. Not able to even think of anything....


// heater update
// This is the array which is coming from the form
/*
    heatername
  Array ( [0] => ehiem 
            [1] => Dolphin 
            [2] => Rena 
            [3] => jobo ) 

 heaterimg
    Array ( [0] => img1.jpg 
            [1] => img2.jpg 
            [2] => img3.jpg 
            [3] => img4.jpg )
*/

$heatername = mysql_real_escape_string($heatername);
$heaterimgpath = mysql_real_escape_string($heaterimg);
$combined_array = array_combine($heatername, $heaterimgpath);
$values = array();
foreach ($combined_array as $heatername => $heaterimgpath)
{
    $values[] = "('$heatername', '$heaterimgpath')";
}
$sql = "INSERT INTO heater (heater , imgpath) VALUES " . implode(', ', $values);
//echo $lastid = mysql_insert_id()."<br />";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "records added<br />";

//Product heater Update table
    // This is where Im stuck. Not able to even think of anything....

我的问题是:如何更新product_filter &产品加热器表

Need help in updating the tables

I have array of data coming from a form which I'm inserting in my DB. I have 5 tables

  • product
  • filter
  • product_filter
  • heater
  • product_heater

Im able to put the data in the "product", "filter" & "heater" tables but i dont know how to put data inside the "product_filter" & "product_heater" table. Any help or direction to any tutorials is appreciated.

My Tables structure:

product

  • id int(5)
  • product text
  • cost text
  • details text

filter

  • id int(5)
  • filter text
  • imgpath text

product_filter

  • id int(5)
  • id_product int(5)
  • id_filter int(5)

heater

  • id int(5)
  • heater text
  • imgpath text

product_heater

  • id int(5)
  • id_product int(5)
  • id_heater int(5)

I want to have

  • many-to-many relation between product & heater
  • many-to-many relation between product & Filter

Here is the code :

PHP Syntax (Toggle Plain Text)
// Product data Update
$name = mysql_real_escape_string($_POST['product']);
$cost = mysql_real_escape_string($_POST['cost']);
$details = mysql_real_escape_string($_POST['details']);

$sql_title = "INSERT INTO product (
            id ,
            product ,
            cost ,
            details ,
            )
            VALUES (
            NULL , '$name' , '$cost' , '$details')";
if (!mysql_query($sql_title,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "records for product added<br />";         


// Filter update
// This is the array which is coming from the form
/*
    filtername
   Array ( [0] => ehiem 
            [1] => Hagan 
            [2] => Rena 
            [3] => jobo ) 

 filterimg
    Array ( [0] => img1.jpg 
            [1] => img2.jpg 
            [2] => img3.jpg 
            [3] => img4.jpg )
*/

$filtername = mysql_real_escape_string($filtername);
$filterimgpath = mysql_real_escape_string($filterimg);
$combined_array = array_combine($filtername, $filterimgpath);
$values = array();
foreach ($combined_array as $filtername => $filterimgpath)
{
    $values[] = "('$filtername', '$filterimgpath')";
}
$sql = "INSERT INTO filter (filter , imgpath) VALUES " . implode(', ', $values);
//echo $lastid = mysql_insert_id()."<br />";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "records added<br />";

//Product Filter Update table
    // This is where Im stuck. Not able to even think of anything....


// heater update
// This is the array which is coming from the form
/*
    heatername
  Array ( [0] => ehiem 
            [1] => Dolphin 
            [2] => Rena 
            [3] => jobo ) 

 heaterimg
    Array ( [0] => img1.jpg 
            [1] => img2.jpg 
            [2] => img3.jpg 
            [3] => img4.jpg )
*/

$heatername = mysql_real_escape_string($heatername);
$heaterimgpath = mysql_real_escape_string($heaterimg);
$combined_array = array_combine($heatername, $heaterimgpath);
$values = array();
foreach ($combined_array as $heatername => $heaterimgpath)
{
    $values[] = "('$heatername', '$heaterimgpath')";
}
$sql = "INSERT INTO heater (heater , imgpath) VALUES " . implode(', ', $values);
//echo $lastid = mysql_insert_id()."<br />";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "records added<br />";

//Product heater Update table
    // This is where Im stuck. Not able to even think of anything....

my question is : how to update product_filter & product_heater tables ?

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

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

发布评论

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

评论(1

百变从容 2024-12-30 16:43:11

您正在将数据插入产品表中。
并且需要获取刚刚插入的产品的auto_increment_id。
如果您插入多个项目,这会变得很棘手。

然而,您只有一个产品条目,所以这很容易:

插入产品后添加以下内容:

echo "records for product added<br />";    
$get_product_id = "SELECT LAST_INSERT_ID() as product_id";
$result = mysql_query($get_product_id);
$row = mysql_fetch_array($result);
$product_id = $row['product_id'];
echo "product_id is: ".$product_id;

获取加热器值的一种方法是向加热器表添加触发器:

DELIMITER $

CREATE TRIGGER ai_heater_each AFTER INSERT ON heater FOR EACH ROW
BEGIN
  INSERT INTO heater_inserts (heater_id, conn_id) VALUES (new.id, CONNECTION_ID());
END $

DELIMITER ;

这会将所有加热器 ID 存储到临时表中,以及当前连接的 ID。
这可以确保您在使用其他客户端的插入时不会遇到并发问题。

您可以像这样插入product_heater 表:

INSERT INTO product_heater (id_product, id_heater) 
SELECT $product_id, hi.heater_id FROM heater_inserts hi
WHERE hi.conn_id = CONNECTION_ID();

完成后不要忘记清理heater_inserts 表。

DELETE FROM heater_inserts WHERE conn_id = CONNECTION_ID();

您可以使用临时表,它不需要清理,也不需要 connection_id,因为它们在每个会话中都是唯一的,但在这种情况下,您需要在会话开始时创建该表。
另请注意,如果连接断开,临时表会被破坏,因此您需要添加一些样板代码来处理该问题。

创建表时可以使用 TEMPORARY 关键字。 TEMPORARY 表仅对当前连接可见,并在连接关闭时自动删除。这意味着两个不同的连接可以使用相同的临时表名称,而不会相互冲突或与现有的同名非 TEMPORARY 表发生冲突。 (现有表将被隐藏,直到删除临时表。)要创建临时表,您必须具有 CREATE TEMPORARY TABLES 权限。

You are inserting data into the product tables.
And you need to get the auto_increment_id of the products you just inserted.
This gets tricky if you insert multiple items.

However you have a single entry for the product, so that's easy:

Add the following after you insert your product:

echo "records for product added<br />";    
$get_product_id = "SELECT LAST_INSERT_ID() as product_id";
$result = mysql_query($get_product_id);
$row = mysql_fetch_array($result);
$product_id = $row['product_id'];
echo "product_id is: ".$product_id;

One way of getting the heater values is to add a trigger to your heater table:

DELIMITER $

CREATE TRIGGER ai_heater_each AFTER INSERT ON heater FOR EACH ROW
BEGIN
  INSERT INTO heater_inserts (heater_id, conn_id) VALUES (new.id, CONNECTION_ID());
END $

DELIMITER ;

This will store all heater ids into a temp table, along with the id of the current connection.
This makes sure you don't run into concurrency issues where you use the inserts from other clients.

You can insert the product_heater table like so:

INSERT INTO product_heater (id_product, id_heater) 
SELECT $product_id, hi.heater_id FROM heater_inserts hi
WHERE hi.conn_id = CONNECTION_ID();

Don't forget to clean up the heater_inserts table when you're done.

DELETE FROM heater_inserts WHERE conn_id = CONNECTION_ID();

You can use temporary tables, which don't require cleanup and don't need a connection_id because they're unique per session, but in that case you need to create the table at the start of the session.
Also note that a temp table gets destroyed if the connection drops, so you'll need to add some boilerplate code to deal with that.

You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.) To create temporary tables, you must have the CREATE TEMPORARY TABLES privilege.

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