2013-03-24 112 views
0

我试图从Web上的XML文件中将数据导入到我的数据库中,以便我可以使用它。将XML文件中的数据插入到表中

我已经生成了以下代码,但是由于我已经完成了很长时间的编码工作,所以我收到了错误消息。

错误是“字段列表”中的“未知列'10074'”。

10074是XML文件中第一项的产品ID。

任何指针都会非常有用,因为它正在做我的头!

我的代码如下:

<?php 
    $Products = simplexml_load_file('http://atsdistribution.co.uk/feeds/xml_all_products.aspx'); 


$con = mysql_connect(Details); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
mysql_select_db("catflaps_products", $con); 


foreach($Products->Product as $Product) 
{ 
$ProductID = $Product->ProductID; 
$Name = $Product->Name; 
$DropshipPrice = $Product->DropshipPrice; 
$SRP = $Product->SRP; 
$Brand = $Product->Brand; 
$Xline = $Product->Xline; 
$InStock = $Product->InStock; 
$Stock = $Product->Stock; 
$Barcode = $Product->Barcode; 
$Weight = $Product->Weight; 
$CategoryID = $Product->CategoryID; 
$Category = $Product->Category; 
$SmallImage = $Product->SmallImage; 
$LargeImage = $Product->LargeImage; 
$Description = $Product->Description; 

mysql_query("INSERT INTO test(ProductID, Name, DropshipPrice, SRP, Brand, Xline, InStock, Stock, Barcode, Weight, CategoryID, Category, SmallImage, LargeImage, Description) 
VALUES(`$ProductID`, `$Name` , `$DropshipPrice`, `$SRP`, `$Brand`, `$Xline`, `$InStock`, `$Stock`, `$Barcode`, `$Weight`, `$CategoryID`, `$Category`, `$SmallImage`, `$LargeImage`, `$Description`)") 
     or die(mysql_error()); 

} 

mysql_close($con); 


?> 

回答

0
  1. 你不应该使用反引号内VALUES部分。这只是引用mysql标识符(如表,列名称)。我认为,如果你删除它您的问题将得到解决
  2. 您应该使用引号(普通的人是'或“),当你引用在VALUES部分字符串值(但看到下面,有一个更好的方法)
  3. 如果您选择#2,那么你需要在你的案例中使用mysql_real_escape_string来正确地从XML中跳出你的值,事实上,如果你不这样做,这是一个安全漏洞(参见SQL注入),但即使你这样说临时脚本一次性使用等等,当xml数据中有单引号或双引号时,您可能会遇到另一个错误
  4. 最好的方法是使用PDO准备语句,然后您不打扰引用某些数据类型带引号或不使用 - 你将某个参数与其数据类型绑定在一起,并记住m ysql_ *函数今天已弃用。

所以这段代码就像一个魅力:

<?php 
$Products = simplexml_load_file('xml_all_products.xml'); 
$config = array('db' => array(
    'dbname' => 'test', 
    'host' => 'localhost:4040', 
    'username' => 'xx', 
    'password' => 'xxx' 
)); 
$db = new PDO('mysql:dbname='.$config['db']['dbname'].';host='.$config['db']['host'],$config['db']['username'],$config['db']['password']); 

foreach($Products->Product as $Product) 
{ 
    $ProductID = $Product->ProductID; 
    $Name = $Product->Name; 
    $DropshipPrice = $Product->DropshipPrice; 
    $SRP = $Product->SRP; 
    $Brand = $Product->Brand; 
    $Xline = $Product->Xline; 
    $InStock = $Product->InStock; 
    $Stock = $Product->Stock; 
    $Barcode = $Product->Barcode; 
    $Weight = $Product->Weight; 
    $CategoryID = $Product->CategoryID; 
    $Category = $Product->Category; 
    $SmallImage = $Product->SmallImage; 
    $LargeImage = $Product->LargeImage; 
    $Description = $Product->Description; 

    $ProductsRS = $db->prepare("INSERT INTO test(ProductID, Name, DropshipPrice, SRP, Brand, Xline, InStock, Stock, Barcode, Weight, CategoryID, Category, SmallImage, LargeImage, Description) 
         VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); 

    $ProductsRS->execute(array($ProductID, $Name, $DropshipPrice, $SRP, $Brand, $Xline, $InStock, $Stock, $Barcode, $Weight, $CategoryID, $Category, $SmallImage, $LargeImage, $Description)); 
} 
+0

太谢谢你了!这真是一种魅力! 我已经离开游戏太久了!干杯! – Ian 2013-03-24 18:58:14

相关问题