如何将XML文件插入Wordpress数据库

时间:2015-10-28 10:07:16

标签: php mysql xml wordpress

我有一个here的XML数据Feed。

我正在尝试使用此功能将所有产品插入数据库或更新重复键。重复键是产品元素<prod id="685814171" in_stock="yes" stock_quantity="">上的ID。我将此保存为SKU字段。

这是功能

function parse_xml(){
global $wpdb;
$xml = simplexml_load_file('http://www.hot-offers.net/wp-content/themes/hotoffers/datafeed_249713.xml');

foreach($xml->datafeed->prod as $item){

    $att = $item->attributes();

    $sql = 'INSERT INTO wp_pcu_babytoddler_products 
              (sku, title, url, stock_message, price, image, pid) 
            VALUES
              (%d, %s, %s, %s, %d, %s, %s) 
            ON DUPLICATE KEY UPDATE    
              title = VALUES(title),
              url = VALUES(url),
              stock_message = VALUES(stock_message),
              price = VALUES(price),
              image = VALUES(image),
              pid = VALUES(pid);';

    $sql_prep = $wpdb->prepare($sql,
                        (string)$att->id,
                        (string)$item->text->name,
                        (string)$item->uri->awTrack,
                        (string)$att->in_stock,
                        str_replace('.','',(string)$item->price->buynow),
                        (string)$item->uri->mImage,
                        (string)$item->pId);

    $wpdb->query($sql_prep);
}
}

这是我的数据库架构

CREATE TABLE IF NOT EXISTS `wp_pcu_babytoddler_products` (
  `sku` int(11) NOT NULL,
  `title` text NOT NULL,
  `url` text NOT NULL,
  `stock_message` text NOT NULL,
  `price` int(11) NOT NULL,
  `image` text NOT NULL,
  `pid` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `wp_pcu_babytoddler_products` ADD PRIMARY KEY (`sku`);

此功能部分有效,因为它将 136 行插入到数据库中,但应该有 232 ,因为Feed中有。我做错了什么?

我不必使用$wpdb对象来插入这些,我只是觉得它更容易。

修改

虽然ID 2147483647并不存在于XML Feed中,但这是一个错误,这似乎影响了剩余的产品。

WordPress database error: [Duplicate entry '2147483647' for key 'sku']
INSERT INTO `wp_pcu_babytoddler_products` 
    (`sku`, `title`, `url`, `stock_message`, `price`, `image`, `pid`) 
VALUES 
    (
    '3083667637', 
    'Baby Dan BabyDen Play Pen Black 2014', 
    'http://www.awin1.com/pclick.php?p=3083667637&a=249713&m=3975', 
    'In Stock', 
    '9849', 
    'http://www.babyandtoddlerworld.co.uk/imagprod/imaglarg/BDAN-Babyden-Black.jpg', 
    'BDAN-Babyden-Black-67116-2600-1400-10'
    )

Array
(
    [sku] => 3083667637
    [title] => Baby Dan BabyDen Play Pen Black 2014
    [url] => http://www.awin1.com/pclick.php?p=3083667637&amp;a=249713&amp;m=3975
    [stock_message] => In Stock
    [price] => 9849
    [image] => http://www.babyandtoddlerworld.co.uk/imagprod/imaglarg/BDAN-Babyden-Black.jpg
    [pid] => BDAN-Babyden-Black-67116-2600-1400-10
)

1 个答案:

答案 0 :(得分:1)

刚刚发现问题,数据库中的SKU为int()int()可存储的最大数量为 2147483647 。它会截断任何数字!

我按照这个回答:https://stackoverflow.com/a/20442152/1838483

您只需将varchar(20)更改为@EnableWebMvc @Configuration @ComponentScan("com.springapp.mvc") public class MvcConfig extends WebMvcConfigurerAdapter { ... @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/pages/"); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setStatusCode(HttpStatus.MOVED_PERMANENTLY).set‌​ViewName("forward:/index.html"); } ... }