【问题标题】:Inserting multidimensional Array into existing SQL database将多维数组插入现有 SQL 数据库
【发布时间】:2015-12-21 05:55:51
【问题描述】:

我有一个大型 xml 文件,我正在使用 XML 阅读器收集特定信息。一旦我将所有信息存储在数组 $content 中,然后将其插入另一个数组 $preOwnedData。使其成为一个多维数组。

我现在正在尝试使用 PHP 将数组数据插入到已经存在的 SQL 数据库中,但是我目前无处可去!我知道用 MYSQLI 插入的标准方法。

我假设对于数组,您必须执行某种循环才能通过数组。

数据库已经建立,我基本上需要将数组中的字符串插入到其中示例:

INSERT INTO $table (id) VALUES (DocumentID) 等我需要的所有值。

这是阵列的 var_dump 的一部分

array (size=69)
  0 => 
    array (size=6)
      'DocumentID' => string '898052' (length=6)
      'ChargeAmount' => string '31500' (length=5)
      'URI' => string 'http://imt.boatwizard.com/images/1/80/52/898052_0_040820101331_4.jpg' (length=68)
      'MakeString' => string 'Cranchi' (length=7)
      'ModelYear' => string '2001' (length=4)
      'Model' => string 'Perla 25' (length=8)
  1 => 
    array (size=6)
      'DocumentID' => string '898052' (length=6)
      'ChargeAmount' => string '31500' (length=5)
      'URI' => string 'http://imt.boatwizard.com/images/1/80/52/898052_0_040820101331_4.jpg' (length=68)
      'MakeString' => string 'VOLVO' (length=5)
      'ModelYear' => string '2001' (length=4)
      'Model' => string '4.3 L' (length=5)

PHP 代码已更正此代码现在可以使用

$url="https://services.boatwizard.com/bridge/events/38acaac6-c5c6-4aa6-a40c-41fbc1296515/boats?status=on";
$xml = new XMLReader();
$xml->open($url);

$PreOwnedData = array();

while($xml->read()){
    //id
    if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'DocumentID'){
        $content = array();
        $xml->read();
        $content['DocumentID'] = $xml->value;
    }
    //price
    if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'ChargeAmount'){
        $xml->read();
        $content['ChargeAmount'] = $xml->value;
    }

    //img
    if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'URI'){
        $xml->read();
        $content['URI'] = $xml->value;
    }

    //make
    if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'MakeString'){
        $xml->read();
        $content['MakeString'] = $xml->value;
    }
    //year
    if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'ModelYear'){
        $xml->read();
        $content['ModelYear'] = $xml->value;
    }
    //model
    if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'Model'){
        $xml->read();
        $content['Model'] = $xml->value;
        $PreOwnedData[] = $content;
    }

}

//Paramaters for SQL connections  LOCAL
    $hostname="localhost";
    $username="root";
    $password="";
    $database="test";
    $table="preowned";

$conn = mysqli_connect($hostname,$username,$password,$database);

//Check connection
    if (!$conn) {
        die("Connection failed:".mysqli_connect_error());
    }

    if(count($PreOwnedData) > 0){
        foreach($PreOwnedData as $content){
            $id = $content['DocumentID'];
            $sql = "INSERT INTO $table (id) VALUES ('$id')";
            if (mysqli_query($conn, $sql)) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }
    }

【问题讨论】:

  • 那么 - mysqli_query 在哪里?你说你知道用 MYSQLI 插入的标准方法。那有什么问题呢?
  • 抱歉,最后一段代码没有复制过来。基本上没有任何东西被发送到数据库。我猜您将数组信息发送到数据库的方式不同?
  • 所以,你声明了insertoDB 函数。你怎么称呼它?
  • 感谢您指出我没有调用该函数! @u_mulder
  • 解决了这个问题!感谢 Adelphia 发现了向后变量。删除该功能解决了这个问题。我将使用正确的代码更新问题。感谢所有的帮助!

标签: php mysql arrays multidimensional-array mysqli


【解决方案1】:
  1. 您没有调用实际执行插入的函数。调用它。
  2. 在您的函数中,$table 未定义。定义它。
  3. 另外,这是倒退..$content['DocumentID'] = $id;。交换它。

祝你好运!

【讨论】:

  • 谢谢提醒!简单的错误应该已经注意到了!
猜你喜欢
  • 2021-07-25
  • 2011-12-06
  • 2017-05-18
  • 2015-06-08
  • 2017-06-21
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多