如何将2个数组插入到mysql数据库中的2列,不同的行?

时间:2012-12-31 09:01:10

标签: php mysql database

foreach ($_SESSION['exampleOne'] as $item)
{//insert sql

foreach ($_SESSION['exampleTwo'] as $comment)
{//insert sql

如何将2个foreach合并在一起? 现在我的数据库商店就像这样......:

    comment | item
    ---------------
    Nice!   | Pants
    Great!  | Pants
    Awesome!| Pants
    Nice!   | Skirts
    Great!  | Skirts
    Awesome!| Skirts
    Nice!   | Shirts
    Great!  | Shirts
    Awesome!| Shirts

当我存储它时,我希望它在我的数据库中以某种方式存在:

comment | item
---------------
Nice!   | Pants
Great!  | Skirts
Awesome!| Shirts

1 个答案:

答案 0 :(得分:1)

你可以这样做 -

$merge = array_combine ($_SESSION['exampleOne'],$_SESSION['exampleTwo'])
foreach($merge as $key => $value)
{
    // your insert query
    $query = "insert into tableName set comment='$key', item='$value'";
}