XML数据到我的数据库中

时间:2013-03-24 18:23:13

标签: php mysql xml database

自从我成为一名程序员以来,我已经有很长一段时间了 我知道这很简单。

我正在尝试访问产品的XML页面并将它们插入到我的数据库中。

我生成的代码给出了以下错误消息:

Unknown column '10074' in 'field list'  

10074是第一项的产品ID。

代码如下。

你能否指出我正确的方向,因为它真的让我感动。

非常感谢提前!

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


$con = mysql_connect(*****);
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);
// some code

?>

2 个答案:

答案 0 :(得分:0)

很可能是解析xml。确保调用mysql_real_escape_string来清理数据。

http://php.net/manual/en/function.mysql-real-escape-string.php

答案 1 :(得分:0)

您使用'反引号'(`)来引用您的值,因此MySQL将它们视为'列名'。使用常规的“单个”引号(')应该可以解决您的问题

相关问题