2013-03-24 34 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

相關問題