0

值位于多维数组中,例如:

Array ( 
    [0] => Array (
        [fname] => CollectiveAccess
        [lname] => Administrator
        [username] => administrator
        )
    [1] => Array (
        [fname] => Jorge
        [lname] => Ghelman 
        [username] => jorgeghe 
        )
    [2] => Array (
        [fname] => Sebastian
        [lname] => Scarano
        [username] => sscarano
        )
)

这是我用来构建查询的循环:

foreach ($user_names as $value) {
    foreach ($value as  $value2) {
        echo $value2 . " "; //prints all values
        $query = "INSERT INTO users (username, fname, lname)
                  VALUES ('$value2', );";
    }
}

print_r($query) 显示只有 $value2 中的最后一个值被传递给查询。

INSERT INTO users (username, fname, lname) VALUES ('test1', ); 

我确定这是一个菜鸟错误,但无法弄清楚。谢谢

4

1 回答 1

1

尝试这个

foreach ($user_names as $value) {
    $query = "INSERT INTO users (username, fname, lname)
              VALUES ('{$value['username']}', '{$value['fname']}', '{$value['lname']}');";
    echo $query;
}
于 2013-08-17T18:59:33.027 回答