我有一个名为'客户'和五个阵列,名称,地址,城市,帽子,国家。在我的数据库中,我想创建包含有关每个客户的此信息的10条记录。使用我的代码,所有数据都是isert进入数据库,但首先插入所有名称,所有地址等等,所以我得到50条记录而不是10条。我该如何解决?
<?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();
?>
答案 0 :(得分:1)
1)连接数据库 ONCE 。您不必每次都在每个循环中连接
2)使用 SINGLE 循环和 SINGLE 查询:
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)阅读并了解sql injection attacks,因为您已经敞开心扉来获取服务器pwn3d。
答案 1 :(得分:0)
因为你在每个数组上循环,并插入所有数组。
试试这个:
$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();
如果我可以向您提出建议:在这样的多维数组中组织这样的数据:
$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...
);
更容易维护和操纵。
答案 2 :(得分:0)
你应该只有一个循环。编写for循环并使用索引/计数器访问数组中的值。像这样:
for ($i = 0, $i < 10, $i++) {
//insert using index $name[$i], $address[$i] etc.
}