Php 使用foreach 2级阵列循环进行多次插入
我使用Php 使用foreach 2级阵列循环进行多次插入,php,mysql,foreach,insertion,Php,Mysql,Foreach,Insertion,我使用foreach进行多次插入(有两级循环,因为每个产品可能有多个属性)。建议使用stmt,但不确定如何使用 我知道从表单中检索数据的方法。我需要帮助将数据放入数据库 Array ( [1] => Array ( [category] => 1 [code] => NFK50889922 [price] => 15.00 [name] => Pendants [description] => Gold pendants covered with 400
foreach
进行多次插入(有两级循环,因为每个产品可能有多个属性)。建议使用stmt,但不确定如何使用
我知道从表单中检索数据的方法。我需要帮助将数据放入数据库
Array ( [1] => Array (
[category] => 1
[code] => NFK50889922
[price] => 15.00 [name] => Pendants
[description] => Gold pendants covered with 400k diamond
[thumbnail] => 131120091585.jpg
//second level array for attribute
[attcode] => Array ( [0] => [1] => [2] => )
[color] => Array ( [0] => [1] => [2] => )
[size] => Array ( [0] => [1] => [2] => )
[stock] => Array ( [0] => [1] => [2] => ) ) )
代码:
prodcut表:
id---code---name---description---categori_id---price
产品属性表:
id---product_id---code---color---size---stock
在mysql中,您可以一次插入多行:
INSERT INTO TableName(
foo_field,
bar_field
)
VALUES
( foo1, bar1 ),
( foo2, bar2 ),
( foo3, bar3 ),
( foo4, bar4 )
这种方法的缺点是,您无法使用预先准备好的语句,从而获得针对注入的内置保护的额外好处
或者,您可以创建一个准备好的语句,然后在循环中使用参数执行它。这将是一种较慢的方法,但在插入数据之前,您不需要手动清理数据。在mysql中,您可以一次插入多行:
INSERT INTO TableName(
foo_field,
bar_field
)
VALUES
( foo1, bar1 ),
( foo2, bar2 ),
( foo3, bar3 ),
( foo4, bar4 )
这种方法的缺点是,您无法使用预先准备好的语句,从而获得针对注入的内置保护的额外好处
或者,您可以创建一个准备好的语句,然后在循环中使用参数执行它。这将是一种较慢的方法,但在插入数据之前,您不需要手动清理数据。如果您的
$product
数组如下所示:
Array
(
[0] => Array
(
[name] => thename1
[color] => thecolor1
[size] => thesize1
[stock] => thestock1
[attcode] => theattcode1
)
[1] => Array
(
[name] => thename2
[color] => thecolor2
[size] => thesize2
[stock] => thestock2
[attcode] => theattcode2
)
)
<?php
foreach($product as $k=>$v)
{
$name = $product[$k]['name'];
$color = $product[$k]['color'];
$size = $product[$k]['size'];
$stock = $product[$k]['stock'];
$attcode = $product[$k]['attcode'];
$mysqli->query('INSERT INTO table(product_id,code,color,size,stock) VALUES(....,....,....,...,...)');
}
?>
然后你可以这样做:
Array
(
[0] => Array
(
[name] => thename1
[color] => thecolor1
[size] => thesize1
[stock] => thestock1
[attcode] => theattcode1
)
[1] => Array
(
[name] => thename2
[color] => thecolor2
[size] => thesize2
[stock] => thestock2
[attcode] => theattcode2
)
)
<?php
foreach($product as $k=>$v)
{
$name = $product[$k]['name'];
$color = $product[$k]['color'];
$size = $product[$k]['size'];
$stock = $product[$k]['stock'];
$attcode = $product[$k]['attcode'];
$mysqli->query('INSERT INTO table(product_id,code,color,size,stock) VALUES(....,....,....,...,...)');
}
?>
如果您的
$product
数组如下所示:
Array
(
[0] => Array
(
[name] => thename1
[color] => thecolor1
[size] => thesize1
[stock] => thestock1
[attcode] => theattcode1
)
[1] => Array
(
[name] => thename2
[color] => thecolor2
[size] => thesize2
[stock] => thestock2
[attcode] => theattcode2
)
)
<?php
foreach($product as $k=>$v)
{
$name = $product[$k]['name'];
$color = $product[$k]['color'];
$size = $product[$k]['size'];
$stock = $product[$k]['stock'];
$attcode = $product[$k]['attcode'];
$mysqli->query('INSERT INTO table(product_id,code,color,size,stock) VALUES(....,....,....,...,...)');
}
?>
然后你可以这样做:
Array
(
[0] => Array
(
[name] => thename1
[color] => thecolor1
[size] => thesize1
[stock] => thestock1
[attcode] => theattcode1
)
[1] => Array
(
[name] => thename2
[color] => thecolor2
[size] => thesize2
[stock] => thestock2
[attcode] => theattcode2
)
)
<?php
foreach($product as $k=>$v)
{
$name = $product[$k]['name'];
$color = $product[$k]['color'];
$size = $product[$k]['size'];
$stock = $product[$k]['stock'];
$attcode = $product[$k]['attcode'];
$mysqli->query('INSERT INTO table(product_id,code,color,size,stock) VALUES(....,....,....,...,...)');
}
?>
你尝试过什么吗?我尝试过使用stmt插入,但我想知道是否有更好的方法。你尝试过什么吗?我尝试使用stmt插入,但我想知道是否有更好的方法。请停止推广古老的mysql.*
函数。它们正在被弃用,您不应该编写任何包含它们的新代码。相反,你应该学习如何使用或使用准备好的语句。请停止推广古老的mysql.*
函数。它们正在被弃用,您不应该编写任何包含它们的新代码。相反,你应该学习如何使用或处理准备好的语句。