如何将数组插入MYSQL数据库?

时间:2011-09-15 23:02:30

标签: php mysql

这是我的十大电影阵列

$data() = Array ( [0] => Array ( [rank] => 1. [rating] => 9.2 [title] => The Shawshank Redemption [year] => 1994 [number_of_votes] => 643,339 ) [1] => Array ( [rank] => 2. [rating] => 9.2 [title] => The Godfather [year] => 1972 [number_of_votes] => 488,694 ) [2] => Array ( [rank] => 3. [rating] => 9.0 [title] => The Godfather: Part II [year] => 1974 [number_of_votes] => 301,665 ) [3] => Array ( [rank] => 4. [rating] => 8.9 [title] => The Good, the Bad and the Ugly [year] => 1966 [number_of_votes] => 202,341 ) [4] => Array ( [rank] => 5. [rating] => 8.9 [title] => Pulp Fiction [year] => 1994 [number_of_votes] => 507,363 ) [5] => Array ( [rank] => 6. [rating] => 8.9 [title] => 12 Angry Men [year] => 1957 [number_of_votes] => 154,104 ) [6] => Array ( [rank] => 7. [rating] => 8.9 [title] => Schindler's List [year] => 1993 [number_of_votes] => 337,187 ) [7] => Array ( [rank] => 8. [rating] => 8.8 [title] => One Flew Over the Cuckoo's Nest [year] => 1975 [number_of_votes] => 265,303 ) [8] => Array ( [rank] => 9. [rating] => 8.8 [title] => The Dark Knight [year] => 2008 [number_of_votes] => 578,207 ) [9] => Array ( [rank] => 10. [rating] => 8.8 [title] => Inception [year] => 2010 [number_of_votes] => 415,584 ) )

模式

mysql_query("CREATE TABLE TopMovies (rank INT, 
                                     rating VARCHAR(3), 
                                     title VARCHAR(50), 
                                     year INT, 
                                     number_of_votes INT )");

这是我的循环

foreach ($data as $movie => $rows) {
    foreach ($rows as $row => $columns) {
       $query = "INSERT INTO TopMovies (rank, rating, title, year, number_of_votes)
          VALUES ({$columns['rank']}, {$columns['rating']}, {$columns['title']},
                  {$columns['year']}, {$columns['number_of_votes']});";                             
    }
    mysql_query($query);        
}

这就是我得到的,出了什么问题?

+------+--------+-------+------+-----------------+
| rank | rating | title | year | number_of_votes |
+------+--------+-------+------+-----------------+
|    6 | 6      | 6     |    6 |               6 |
|    4 | 4      | 4     |    4 |               4 |
|    3 | 3      | 3     |    3 |               3 |
|    2 | 2      | 2     |    2 |               2 |
|    5 | 5      | 5     |    5 |               5 |
|    1 | 1      | 1     |    1 |               1 |
|    3 | 3      | 3     |    3 |               3 |
|    2 | 2      | 2     |    2 |               2 |
|    5 | 5      | 5     |    5 |               5 |
|    4 | 4      | 4     |    4 |               4 |
+------+--------+-------+------+-----------------+

3 个答案:

答案 0 :(得分:2)

鉴于这些数据,我相信这是你需要的循环:

foreach ($data as $movie) {
   $query = "INSERT INTO TopMovies (rank, rating, title, year, number_of_votes)
      VALUES ({$movie['rank']}, '{$movie['rating']}', '{$movie['title']}',
              {$movie['year']}, {$movie['number_of_votes']});";
    mysql_query($query);
}

还要注意mysql_real_escape_string是字符串的数据,可以由用户输入。

答案 1 :(得分:2)

$query = "INSERT INTO TopMovies (rank, rating, title, year, number_of_votes) VALUES "
foreach ($data as $movie => $rows) {
    foreach ($rows as $row => $columns) {
       $query += "({$columns['rank']}, '{$columns['rating']}', '{$columns['title']}',
              {$columns['year']}, {$columns['number_of_votes']}),";                             
    }      
}
#remove last comma add ';'
mysql_query($query);

答案 2 :(得分:0)

你需要将mysql_query放在循环中,然后执行它们。 另外,你有一系列记录。你只需要解析一次。你不需要两个循环。

所以

foreach ($data as $record) {
    $sql = "INSERT INTO .... {$record['rank']}, ...";
    mysql_query($sql);
}