php - 更新多个表 : many-to-many relation

标签 php mysql many-to-many

需要帮助更新表格

我有一组数据来 self 要插入到我的数据库中的表单。我有 5 张 table

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

我可以将数据放入“product”、“filter”和“heater”表中,但我不知道如何将数据放入“product_filter”和“product_heater”表中。感谢对任何教程的任何帮助或指导。

我的表结构:

产品

  • id 整数(5)
  • 产品文字
  • 成本文本
  • 详细信息

过滤器

  • id 整数(5)
  • 过滤文字
  • imgpath 文本

product_filter

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

加热器

  • id 整数(5)
  • 加热器文本
  • imgpath 文本

产品加热器

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

我想拥有

  • 产品加热器之间的多对多关系
  • productFilter 之间的多对多关系

代码如下:

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 和 product_heater 表

最佳答案

您正在向产品表中插入数据。
并且需要获取刚刚插入的产品的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 因为它们在每个 session 中都是唯一的,但在这种情况下您需要在 session 开始时创建表。
另请注意,如果连接断开,临时表会被破坏,因此您需要添加一些样板代码来处理这个问题。

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.

关于php - 更新多个表 : many-to-many relation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8703314/

相关文章:

PHP MySQL 获得前五名最高结果。?

java - Liferay 和 Alfresco 的任何 PHP 或 Python 替代品?

javascript - 单击我的提交按钮时自动滚动

mysql - 作为(动态)列值的 SQL 查询

python - django:如何在 ManyToManyField(通过)相关模型中的字段上对查询集进行排序

适用于 facebook 风格帖子的 mySQL 表设计,适合一对多或多对多

php - "Afterware"对于 Laravel

mysql - 带公式的SQL语句

mysql - 查询以在 HQL 中获取超过 6500 条记录

sql - 如何设计数据库约束,以便两个实体只有在两个字段值匹配时才能具有多对多关系?