Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将多维数组信息插入数据库_Php_Mysql_Sql - Fatal编程技术网

Php 将多维数组信息插入数据库

Php 将多维数组信息插入数据库,php,mysql,sql,Php,Mysql,Sql,这似乎是一个非常简单的解决方案……但我一直很难弄明白。我需要一个数组进入数据库。例如: $usa = array( 'idaho' => array( county1 => array( 'pocatello', 'arimo', 'downey' ), county2 => array( 'bear',

这似乎是一个非常简单的解决方案……但我一直很难弄明白。我需要一个数组进入数据库。例如:

$usa = array(
'idaho' => array(
           county1 => array(
                     'pocatello', 'arimo', 'downey'
                      ),
           county2 => array(
                      'bear', 'cuprum', 'mesa'
                      )
'iowa' => array(
           county1 => array(
                     'des moines', 'adel', 'desoto'
                      ),
           county2 => array(
                      'douglas', 'grant', 'jasper'
                      )

);
我尝试了这种插入数据库的方法:

foreach($usa as $state => $county){
    foreach($county as $name => $city){
    $s=$state;
    $county_name = strtolower($name);
    $city = strtolower($city);
    $us = "INSERT INTO us
           SET state='{$s}',county='{$county_name}',city='{$city}'
           ";
    $us_result = mysql_query($us,$connection);
         }
  }
我认为问题在于foreach(将状态变量传递到第二个foreach循环)。我试过很多不同的方法。提前感谢您的帮助


***注意:当我删除插入的$s=$state变量和state='{$s}'部分时,一切都很好。我仍然无法让它插入状态

您的插入查询不正确。 试试这个:

$us = "INSERT INTO us (state, county, city) VALUES ('" . mysql_real_escape_string ($s) . "', '" . mysql_real_escape_string ($county_name) . "', '" . mysql_real_escape_string ($city) . "')";

看来你又错过了一次机会:

foreach($county as $name => $cities) {
 foreach ($cities as $city) {
....

有两个主要问题

首先,数组的分隔不正确,最好对县名称使用单引号或双引号,您还是将其称为字符串:

$usa = array(
  'idaho' => array(
       'county1'=>array(
                 'pocatello', 'arimo', 'downey'
                  ),
       'county2'=>array(
                  'bear', 'cuprum', 'mesa'
                  )),
  'iowa' => array(
       'county1'=>array(
                 'des moines', 'adel', 'desoto'
                  ),
       'county2'=>array(
                  'douglas', 'grant', 'jasper'
                  ))

);
其次,还应该有一个foreach循环来解释城市名称:

foreach($usa as $state => $county){
  foreach($county as $name => $city){
    foreach ($city as $cityname) {
      $s = $state;
      $county_name = strtolower($name);
      $city = strtolower($cityname);
      $us = "INSERT INTO us SET state='{$s}',county='{$county_name}',city='{$city}'";
      echo $us.'<br>';
    }
  }
}
foreach($state=>usa$county){
foreach($country as$name=>$city){
foreach($city作为$cityname){
$s=$state;
$county_name=strtolower($name);
$city=strtolower($cityname);
$us=“INSERT INTO us SET state='{$s}',country='{$country_name}',city='{$city}';
回声$us.“
”; } } }

希望这有帮助

首先。正如@itsmeee所说,您还缺少了一个foreach,该foreach遍历了该县的一系列城市。除此之外,您的输入数组缺少两个闭合参数来包装每个状态的数据数组

如果你解决了这个问题,我会:

$us     = "INSERT INTO us (state, county, city) VALUES ";
$values = array();

foreach($usa as $state => $county){
    foreach($county as $name => $cities){
        foreach($cities as $city){
            $county_name = strtolower($name);
            $city        = strtolower($city);
            $values[]    = "('{$state}','{$county_name}','{$city}')";
        }
    }
}

$us       .= join(',',$values);
$us_result = mysql_query($us,$connection);
这将构建插入字符串,并一次性完成所有插入操作。您也可以进行单独的插入,但对于数据集来说,这样小的插入一个大的插入更有效


祝你好运

+1,虽然你忽略了使用
mysql\u real\u escape\u string进行正确转义的重点,但我更希望提问者一次只学习一些相关的东西,因为他/她似乎是一个PHP程序员新手;如果你再也没有见过这个人,他会继续这样写他的问题;-)好吧,多亏了你,如果他能做到的话,他可能已经研究了什么是mysql\u real\u escape\u string。这是一个很好的提示,但这不是最大的问题:)