更新多个表:多对多关系
需要帮助更新表
我有来自要插入数据库的表单的数据数组。我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在将数据插入产品表中。
并且需要获取刚刚插入的产品的auto_increment_id。
如果您插入多个项目,这会变得很棘手。
然而,您只有一个产品条目,所以这很容易:
插入产品后添加以下内容:
获取加热器值的一种方法是向加热器表添加触发器:
这会将所有加热器 ID 存储到临时表中,以及当前连接的 ID。
这可以确保您在使用其他客户端的插入时不会遇到并发问题。
您可以像这样插入
product_heater
表:完成后不要忘记清理heater_inserts 表。
您可以使用临时表,它不需要清理,也不需要 connection_id,因为它们在每个会话中都是唯一的,但在这种情况下,您需要在会话开始时创建该表。
另请注意,如果连接断开,临时表会被破坏,因此您需要添加一些样板代码来处理该问题。
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:
One way of getting the heater values is to add a trigger to your heater table:
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:Don't forget to clean up the heater_inserts table when you're done.
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.