我想将数组插入表中。
每个名称可以有多个地址。
Name
将位于table1
,所有地址将位于与table2
对应的另一个name
我的数组如下:
array
(
[name] => Array
(
[0] => jack
[1] => jill
[2] => tom
)
[address] => Array
(
[0] => array
(
[0] => jack_aadress1
[1] => jack_aadress2
[2] => jack_aadress3
)
[1] => array(
[0] => jill_address1
[1] => jill_address2
[2] => jill_address3
)
[2] => array(
[0] => tom_address1
[1] => tom_address2
[2] => tom_address3
)
)
以下是我试过的那个:
foreach($_REQUEST['room_name'] as $key=>$value)
{
mysql_query("insert into table1(name) values($value)");
$insert_id = mysql_insert_id();
foreach($_REQUEST['address'] as $key1=>$value1)
{
mysql_query("insert into table2(id,address) values($insert_id,$value1)");
}
}
答案 0 :(得分:1)
你没有深入钻进第二个阵列。它应该是:
foreach($_REQUEST['name'] as $key=>$value)
{
$value = mysql_real_escape_string($value);
mysql_query("insert into table1(name) values('$value')");
foreach($_REQUEST['address'][$key] as $key1=>$value1)
{
$value1 = mysql_real_escape_string($value1);
mysql_query("insert into table2(id,address) values(LAST_INSERT_ID(),'$value1')");
}
}
答案 1 :(得分:0)
foreach($_REQUEST['name'] as $key=>$value)
{
mysql_query("insert into table1(name) values('$value')");
$insert_id = mysql_insert_id();
foreach($_REQUEST['address'][$key] as $key1=>$value1)
{
mysql_query("insert into table2(id,address) values($insert_id, '$value1')");
}
}