dongpai6567
2014-10-30 15:29
浏览 177
已采纳

将数组元素插入数据库字段MYSQL

I have a database called 'customers' and five arrays called name, address, city, cap, country. In my database i want to create 10 records containing this infos about every customer. With my code all the data is isert into database, but first are inserted all the names, the all the addresses and so on, so i got 50 records instead of 10. How can i fix it?

    <?php

$name = array('Alfreds Futterkiste','Ana Trujillo Emparedados','Antonio Moreno Taqueria','Around the Horn','Berglunds snabbkop','Blauer See Delikatessen','Blondel pere et fils ','Bolido Comidas preparadas','Bon app','Bottom-Dollar Marketse');

$address = array('Obere Str. 57','Avda. de la Constitucion 2222','Mataderos 2312','120 Hanover Sq.','Berguvsvagen 8','Forsterstr. 57','24, place Kleber','C/ Araquil, 67','12, rue des Bouchers','23 Tsawassen Blvd');

$city = array('Berlin','Mexico D.F.','Mexico D.F.','London','Lulea','Mannheim','Strasbourg','Madrid','Marseille','Tsawassen');

$cap = array('12209','05021','05023','WA1 1DP','S-958 22','68306','67000','28023','13008','T2F 8M4');

$country = array('Germany','Mexico','Mexico','UK','Sweden','Germany','France','Spain','France','Canada'); 

$conn= mysqli_connect('127.0.0.1','root','','preesame') or die("Connection failed: " . $conn->connect_error);

foreach ($name as $key=>$value)
{
$namess = mysqli_real_escape_string($conn,$value);
$addressess = mysqli_real_escape_string($conn,$addresses);
$citiess = mysqli_real_escape_string($conn,$cities);
$capss = mysqli_real_escape_string($conn,$caps);
$countriess = mysqli_real_escape_string($conn,$countries);
$insert = mysqli_query($conn,"INSERT INTO customers (id,name,address,city,cap,country) VALUES ('',$namess[$key],$addressess[$key],$citiess[$key],$capss[$key],$countriess[$key])");
}

$conn->close();
?>

图片转代码服务由CSDN问答提供 功能建议

我有一个名为'customers'的数据库和五个名为name,address,city,cap,country的数组。 在我的数据库中,我想创建包含有关每个客户的此信息的10条记录。 使用我的代码,所有数据都是isert进入数据库,但首先插入所有名称,所有地址等等,所以我得到50条记录而不是10条。我该如何解决?

 &lt;?php 
 
 $ name = array('Alfreds Futterkiste','Ana Trujillo Emparedados','Antonio Moreno Taqueria','Around the Horn','Berglunds snabbkop','Blauer See  Delikatessen','Blondel pere et fils','Bolido Comidas preparadas','Bon app','Bottom-Dollar Marketse'); 
 
 $ address = array('Obere Str.57','Avda.de la  Constitucion 2222','Mataderos 2312','120 Hanover Sq。','Berguvsvagen 8','Forsterstr.57','24,place Kleber','C / Araquil,67','12,rue des Bouchers',  '23 Tsawassen Blvd'); 
 
 $ city = array('Berlin','Mexico DF','Mexico DF','London','Lulea','Mannheim','Strasbourg','Madrid',  'Marseille','Tsawassen'); 
 
 $ cap = array('12209','05021','05023','WA1 1DP','S-958 22','68306','67000',  '28023','13008','T2F 8M4'); 
 
 $ country = array('Germany','Mexico','Mexico','UK','Sweden','Germany','France'  ,“西班牙”,“法国 ','加拿大');  
 
 $ conn = mysqli_connect('127.0.0.1','root','','preesame')或死亡(“连接失败:”。$ conn-&gt; connect_error); 
 
foreach($ name)  as $ key =&gt; $ value)
 {
 $ namess = mysqli_real_escape_string($ conn,$ value); 
 $ addressess = mysqli_real_escape_string($ conn,$ addresses); 
 $ citiess = mysqli_real_escape_string($ conn,  $ cities); 
 $ capss = mysqli_real_escape_string($ conn,$ caps); 
 $ countriess = mysqli_real_escape_string($ conn,$ countries); 
 $ insert = mysqli_query($ conn,“INSERT INTO customers(id,name)  ,地址,城市,上限,国家)VALUES('',$ namess [$ key],$ addressess [$ key],$ citiess [$ key],$ capss [$ key],$ countrss [$ key])“  ); 
} 
 
 $ conn-&gt; close(); 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongnaosuan5407 2014-10-30 15:33
    已采纳

    Because you are looping on each array, and insert all of them.

    Try this:

    $name = array('Alfreds Futterkiste', 'Ana Trujillo Emparedados', 'Antonio Moreno Taqueria', 'Around the Horn', 'Berglunds snabbkop', 'Blauer See Delikatessen', 'Blondel pere et fils ', 'Bolido Comidas preparadas', 'Bon app', 'Bottom-Dollar Marketse');
    $address = array('Obere Str. 57', 'Avda. de la Constitucion 2222', 'Mataderos 2312', '120 Hanover Sq.', 'Berguvsvagen 8', 'Forsterstr. 57', '24, place Kleber', 'C/ Araquil, 67', '12, rue des Bouchers', '23 Tsawassen Blvd');
    $city = array('Berlin', 'Mexico D.F.', 'Mexico D.F.', 'London', 'Lulea', 'Mannheim', 'Strasbourg', 'Madrid', 'Marseille', 'Tsawassen');
    $cap = array('12209', '05021', '05023', 'WA1 1DP', 'S-958 22', '68306', '67000', '28023', '13008', 'T2F 8M4');
    $country = array('Germany', 'Mexico', 'Mexico', 'UK', 'Sweden', 'Germany', 'France', 'Spain', 'France', 'Canada');
    
    $conn = mysqli_connect('127.0.0.1', 'root', '', 'preesame') or die("Connection failed: " . $conn->connect_error);
    
    $i = 0;
    foreach ($name as $names) {
        $sql = "INSERT INTO customers (name,address,city,cap,country) "
                . "VALUES ('".mysqli_real_escape_string($link, $names[$i])."',"
                . "'".mysqli_real_escape_string($link, $address[$i])."',"
                . "'".mysqli_real_escape_string($link, $city[$i])."',"
                . "'".mysqli_real_escape_string($link, $cap[$i])."',"
                . "'".mysqli_real_escape_string($link, $country[$i])."')";
        $insert = mysqli_query($conn, $sql);
        $i++;
    }
    $conn->close();
    

    And if i can suggesst you a tip: organize your data like this in a multidimensional array like this:

    $persons = array(
        array('name' => 'Alfreds Futterkiste', 'address' => 'Obere Str. 57', 'city' => 'Berlin', 'cap' => 12209, 'country' => 'Germany'),
        array('name' => 'Ana Trujillo Emparedados', 'address' => 'Avda. de la Constitucion 2222', 'city' => 'Mexico D.F.', 'cap' => 05021, 'country' => 'Mexico'),
       //more person here...
    );
    

    Easier to maintaine, and manipulate.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douqulv6059 2014-10-30 15:33

    1) Connect to the database ONCE. You do NOT have to connect each time in each of your loops

    2) Use a SINGLE loop, and a SINGLE query:

    foreach ($name as $key => $value) {
       $sql = "INSERT ... (id, name, address, foo, bar, baz) VALUES ('', $value, $addresses[$key], $foo[$key], $bar[$key], etc...)";
       ... run query here
    }
    

    3) Read up and learn about sql injection attacks because you're wide open for getting your server pwn3d.

    评论
    解决 无用
    打赏 举报
  • dropbox1111 2014-10-30 15:41

    You should only have one loop. Write a for loop and use the index/counter to access the values in your arrays. Something like this:

    for ($i = 0, $i < 10, $i++) {
    //insert using index $name[$i], $address[$i] etc.
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题