简体   繁体   English

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

[英]How to insert array into a MYSQL database?

This is my array for top 10 movies 这是我的十大电影的数组

$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 ) )

Schema 架构

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

This is my loop 这是我的循环

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);        
}

This is what I get, what's wrong? 这就是我得到的,怎么了?

+------+--------+-------+------+-----------------+
| 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 |
+------+--------+-------+------+-----------------+

Given that array of data, I believe this is the loop you need: 给定该数组数据,我相信这是您需要的循环:

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);
}

Also take care to mysql_real_escape_string the data that are strings and may be input by users. 还要注意mysql_real_escape_string数据,这些数据是字符串,可能由用户输入。

$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);

you need to put the mysql_query inside the loop with the queries so you execute them. 您需要将mysql_query与查询放入循环中,以便执行它们。 also, you have an array of records. 此外,您还有一系列记录。 you need only to parse once. 您只需要解析一次。 you don't need two loops. 您不需要两个循环。

so 所以

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM