我正在尝试将Web上的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 :(得分:0)
所以这段代码就像一个魅力:
<?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));
}