Php 将多维数组插入现有SQL数据库
我有一个很大的xml文件,我正在使用xml阅读器收集特定信息。一旦我将所有信息存储在数组$content中,它就会被插入到另一个数组$preOwnedData中。使其成为多维数组 我现在正试图使用PHP将数组数据插入到一个已经存在的SQL数据库中,但是我现在一无所获!我知道使用MYSQLI插入的标准方法 我假设使用数组时,必须进行某种循环才能通过数组 数据库已经设置好了,我基本上需要在其中插入数组示例中的字符串: 为我需要的所有值插入$table(id)值(DocumentID)等等 这是数组的var_转储的一部分Php 将多维数组插入现有SQL数据库,php,mysql,arrays,multidimensional-array,mysqli,Php,Mysql,Arrays,Multidimensional Array,Mysqli,我有一个很大的xml文件,我正在使用xml阅读器收集特定信息。一旦我将所有信息存储在数组$content中,它就会被插入到另一个数组$preOwnedData中。使其成为多维数组 我现在正试图使用PHP将数组数据插入到一个已经存在的SQL数据库中,但是我现在一无所获!我知道使用MYSQLI插入的标准方法 我假设使用数组时,必须进行某种循环才能通过数组 数据库已经设置好了,我基本上需要在其中插入数组示例中的字符串: 为我需要的所有值插入$table(id)值(DocumentID)等等 这是数组的
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;
}
}
}
$url=”https://services.boatwizard.com/bridge/events/38acaac6-c5c6-4aa6-a40c-41fbc1296515/boats?status=on";
$xml=newXMLReader();
$xml->open($url);
$PreOwnedData=array();
而($xml->read()){
//身份证
如果($xml->nodeType==XMLReader::ELEMENT&&$xml->localName==DocumentID')){
$content=array();
$xml->read();
$content['DocumentID']=$xml->value;
}
//价格
如果($xml->nodeType==XMLReader::ELEMENT&&$xml->localName=='ChargeAmount'){
$xml->read();
$content['ChargeAmount']=$xml->value;
}
//img
如果($xml->nodeType==XMLReader::ELEMENT&&$xml->localName=='URI'){
$xml->read();
$content['URI']=$xml->value;
}
//制造
如果($xml->nodeType==XMLReader::ELEMENT&&$xml->localName=='MakeString'){
$xml->read();
$content['MakeString']=$xml->value;
}
//年
如果($xml->nodeType==XMLReader::ELEMENT&&$xml->localName=='ModelYear'){
$xml->read();
$content['ModelYear']=$xml->value;
}
//模型
如果($xml->nodeType==XMLReader::ELEMENT&&$xml->localName==Model'){
$xml->read();
$content['Model']=$xml->value;
$PreOwnedData[]=$content;
}
}
//SQL本地连接的参数
$hostname=“localhost”;
$username=“root”;
$password=“”;
$database=“test”;
$table=“预拥有”;
$conn=mysqli_connect($hostname、$username、$password、$database);
//检查连接
如果(!$conn){
die(“连接失败:”.mysqli_connect_error());
}
如果(计数($PreOwnedData)>0){
foreach($preowneddataas$content){
$id=$content['DocumentID'];
$sql=“插入$table(id)值(“$id”)”;
if(mysqli_查询($conn,$sql)){
echo“新记录创建成功”;
}否则{
echo“Error:”.$sql.“
”$conn->Error;
}
}
}
$table
未定义。定义它祝你好运 那么-mysqli_查询
在哪里?你说过你知道用MYSQLI插入的标准方法。那有什么问题?对不起,最后一段代码没有复制过来。基本上没有任何东西被发送到数据库。我猜您向数据库发送数组信息的方式是不同的?因此,您声明了insertoDB
函数。你在哪里调用它?谢谢你指出我没有调用这个函数@你解决了这个问题!感谢Adelphia发现了向后的变量。删除该函数解决了该问题。我将用正确的代码更新问题。谢谢你的帮助!谢谢你提醒我!简单的错误应该已经注意到了!