如何使用foreach()将xml数据插入mysqli表?

时间:2019-02-02 11:50:53

标签: php html sql xml mysqli

我是新来的,所以请原谅我的错误。所以我有这个问题: 我想将数据从xml文件加载到表中,我正在使用foreach()来获取数据,但是当我尝试将其插入表中时,它总是会写这种错误:

“您的SQL语法有误;请检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在'dec,links,imgurl,priceV,maker,cattxt,ean,prm,prm_N,val附近使用,del_d,del,del'在第1行“

这是我的代码:

<?php 
require ('db.php'); <br/>
set_time_limit (120000);  <br/>
$xmldata1 = @simplexml_load_file('data.xml');

foreach($xmldata1->SHOPITEM as $item)
{  
    $product = (string) trim($item->SHOPITEM);<br/>
    $name = (string) trim($item->PRODUCTNAME);<br/>
    $dec = (string) trim($item->DESCRIPTION);<br/>
    $links = (string) trim($item->URL);<br/>
    $imgurl = (string) trim($item->IMGURL);<br/>
    $priceV = (string) trim($item->PRICE_VAT);<br/>
    $maker = (string) trim($item->MANUFACTURER);<br/>
    $cattxt = (string) trim($item->CATEGORYTEXT);<br/>
    $ean = (string) trim($item->EAN);<br/>
    $prm = (string) trim($item->PARAM);<br/>
    $prm_N = (string) trim($item->PARAM_NAME);<br/>
    $val = (string) trim($item->VAL);<br/>
    $del_d = (string) trim($item->DELIVERY_DATE);<br/>
    $del = (string) trim($item->DELIVERY);<br/>
    $del_pr = (string) trim($item->DELIVERY_PRICE);<br/>
    $itgr_id = (string) trim($item->ITEMGROUP_ID); <br/> 

mysqli_query($con, 'INSERT INTO categories(id, product, name, dec, links,     imgurl, priceV, maker, cattxt, ean, prm, prm_N, val, del_d, del, del_pr,     itgr_id)
VALUES(NULL,"'.mysqli_real_escape_string($con, $product).'","'<br/>
.mysqli_real_escape_string($con,$name).'","'<br/>
.mysqli_real_escape_string($con,$dec).'","'<br/>
.mysqli_real_escape_string($con,$links).'","'<br/>
.mysqli_real_escape_string($con,$imgurl).'","'<br/>
.mysqli_real_escape_string($con,$priceV).'","'<br/>
.mysqli_real_escape_string($con,$maker).'","'<br/>
.mysqli_real_escape_string($con, $cattxt).'","'<br/>
.mysqli_real_escape_string($con,$ean).'","'<br/>
.mysqli_real_escape_string($con,$prm).'","'<br/>
.mysqli_real_escape_string($con,$prm_N).'","'<br/>
.mysqli_real_escape_string($con, $val).'","'<br/>
.mysqli_real_escape_string($con,$del_d).'","'<br/>
.mysqli_real_escape_string($con,$del).'","'<br/>
.mysqli_real_escape_string($con, $del_pr).'","'<br/>
.mysqli_real_escape_string($con, $itgr_id).'")') <br/>
or die(mysqli_error($con));
}  
 ?>

这是我的xml文件的结构:

    <SHOP>
    <SHOPITEM>
    <ITEM_ID> </ITEM_ID>
    <PRODUCTNAME> </PRODUCTNAME>  
    <DESCRIPTION>
    </DESCRIPTION>
    <URL> </URL>  
    <IMGURL> </IMGURL>  
    <PRICE_VAT> </PRICE_VAT> 
     <MANUFACTURER> </MANUFACTURER>
    <CATEGORYTEXT> </CATEGORYTEXT> 
    <EAN> </EAN> 
    <PARAM_NAME> </PARAM_NAME>   
    <VAL> </VAL>
    <PARAM_NAME> </PARAM_NAME>   
    <VAL> </VAL> 
    <PARAM_NAME> </PARAM_NAME>  
    <VAL> </VAL> 
    <PARAM_NAME> </PARAM_NAME>   
    <VAL> </VAL>
    <PARAM_NAME> </PARAM_NAME>  
    <VAL> </VAL>
    <DELIVERY_DATE> </DELIVERY_DATE>
    <PARAM_NAME> </PARAM_NAME>   
    <VAL> </VAL>
    <DELIVERY_DATE> </DELIVERY_DATE> 
    <DELIVERY_PRICE> </DELIVERY_PRICE> 
    <DELIVERY_PRICE_COD> </DELIVERY_PRICE_COD> 
    <DELIVERY_PRICE> </DELIVERY_PRICE>
    <DELIVERY_PRICE_COD> </DELIVERY_PRICE_COD>
    <DELIVERY_PRICE> </DELIVERY_PRICE> 
    <DELIVERY_PRICE_COD> </DELIVERY_PRICE_COD> 
    <DELIVERY_PRICE> </DELIVERY_PRICE> 
    <ITEMGROUP_ID> </ITEMGROUP_ID> 
    </SHOPITEM>
    </SHOP>

这是我的表结构:

CREATE TABLE `categories` (
  `id` varchar(5) COLLATE utf8_bin NOT NULL,
  `product` varchar(20) NOT NULL,
  `name` varchar(70) NOT NULL,
  `dec`  text NOT NULL,
  `links` varchar(70) NOT NULL,
  `imgurl` varchar(40) NOT NULL,
  `priceV` varchar(10) NOT NULL,
  `maker` varchar(20) NOT NULL,
  `cattxt` varchar(20) NOT NULL,
  `ean` varchar(5) NOT NULL,
  `prm` varchar(10) NOT NULL,
  `prm_N` varchar(10) NOT NULL,
  `val` varchar(30) NOT NULL,
  `del_d` varchar(10) NOT NULL,
  `del` varchar(10) NOT NULL,
  `del_pr` varchar(10) NOT NULL,
  `itgr_id` varchar(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8  AUTO_INCREMENT=1;

还有其他(更好)的选项可用于将数据加载到表中吗? 我已经厌倦了加载另一个xml文件(更大和更小),但是结果是相同的。 我问过一些人(和老师)出了什么问题,但没人知道。因此,请任何人告诉我,问题出在哪里?是在mysqli_real_escape_string()函数中,在表结构中还是在xml本身中?非常感谢。

0 个答案:

没有答案