从PHP7

时间:2017-01-23 10:45:01

标签: php xml mysqli

我正在尝试制作一个php文件,将供应商的XML数据源添加到我的产品数据库中(希望以后能让它成为一个cron作业),但我没有很多运气让它起作用:( ..它读取XML文件不是问题。我确实输出了xml文件,但它不想将它插入db中,任何帮助都会非常感激

<?xml version="1.0"?>
<rss version="2.0">
	<channel>
    <language>en-au</language>
    <title> Item Data Feed</title>
    <description> item data feed</description>
    <pubDate>Mon, 23 Jan 2017 16:00:41 GMT</pubDate>
    <item StockCode="AT-CHARGERSTATION-45">
        <CategoryCode>CB</CategoryCode>
        <CategoryName>Cables</CategoryName>
        <SubcategoryID>0</SubcategoryID>
        <SubcategoryName/>
        <StockCode>AT-CHARGERSTATION-45</StockCode>
        <BarCode>9320301002659</BarCode>
        <Weight>0.3400</Weight>
        <ManufacturerSKU>AT-CHARGERSTATION-45</ManufacturerSKU>
        <pubDate>Mon, 23 Jan 2017 16:00:41 GMT</pubDate>
        <ProductName>Astrotek USB Charging Station Charger Hub 3 Port 5V 4A with 1.5m Power Cable White for iPhone Samsung iPad Tablet GPS</ProductName>
        <WarrantyLength>12</WarrantyLength>
        <Manufacturer>Astrotek</Manufacturer>
        <DBP>26.0000</DBP>
        <DBP5>22.0000</DBP5>
        <RRP>47.0000</RRP>
        <StandardRRP>47.0000</StandardRRP>
        <AvailabilityTotal>>5</AvailabilityTotal>
        <AvailabilityAdelaide>>5</AvailabilityAdelaide>
        <AvailabilityBrisbane>>5</AvailabilityBrisbane>
        <AvailabilitySydney>>5</AvailabilitySydney>
        <AvailabilityMelbourne>>5</AvailabilityMelbourne>
        <AvailabilityPerth>>5</AvailabilityPerth>
        <ImageUrl>NDAURL</ImageUrl>
        <WebsiteUrl/>
        <ImageFilename/>
        <DescriptionLink>NDAURL</DescriptionLink>
	</item>

<?php

// read XML data string
// Set File Read or URL READ HERE

	$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
	$url = 'NDA URL HERE';
	
	$xml = file_get_contents($url, false, $context);
	$xml = simplexml_load_string($xml) or die("ERROR: Cannot create SimpleXML object");
	$complete = 'XML Import Completed';
	print_r($complete);

// open MySQL connection
	include ("include/sqlopen.php");
			// sqlopen.php has the following
			//$conn = mysqli_connect('localhost', 'USER', 'PASS', 'DATABASE') or die ("ERROR: Cannot connect");

// process node data
// create and execute INSERT queries
	foreach ($xml->item as $item) {
		
	$StockCode = mysqli_real_escape_string($conn, $item->StockCode);
	$CategoryCode = mysqli_real_escape_string($conn, $item->CategoryCode);
	$CategoryName = mysqli_real_escape_string($conn, $item->CategoryName);
	$SubcategoryID = mysqli_real_escape_string($conn, $item->SubcategoryID);
	$SubcategoryName = mysqli_real_escape_string($conn, $item->SubcategoryName);
	$BarCode = mysqli_real_escape_string($conn, $item->BarCode);
	$Weight = mysqli_real_escape_string($conn, $item->Weight);
	$ManufacturerSKU = mysqli_real_escape_string($item, $ManufacturerSKU->ManufacturerSKU);
	$ProductName = mysqli_real_escape_string($conn, $item->ProductName);
	$WarrantyLength = mysqli_real_escape_string($conn, $item->WarrantyLength);
	$Manufacturer = mysqli_real_escape_string($conn, $item->Manufacturer);
	$DBP = mysqli_real_escape_string($conn, $item->DBP);
	$DBP5 = mysqli_real_escape_string($conn, $item->DBP5);
	$RRP = mysqli_real_escape_string($conn, $item->RRP);
	$StandardRRP = mysqli_real_escape_string($conn, $item->StandardRRP);
	$AvailabilityTotal = mysqli_real_escape_string($conn, $item->AvailabilityTotal);
	$AvailabilityAdelaide = mysqli_real_escape_string($conn, $item->AvailabilityAdelaide);
	$AvailabilityBrisbane = mysqli_real_escape_string($conn, $item->AvailabilityBrisbane);
	$AvailabilitySydney = mysqli_real_escape_string($conn, $item->AvailabilitySydney);
	$AvailabilityMelbourne = mysqli_real_escape_string($conn, $item->AvailabilityMelbourne);
	$AvailabilityPerth = mysqli_real_escape_string($conn, $item->AvailabilityPerth);
	$ImageUrl = mysqli_real_escape_string($conn, $item->ImageUrl);
	$WebsiteUrl = mysqli_real_escape_string($conn, $item->WebsiteUrl);
	$ImageFilename = mysqli_real_escape_string($conn, $item->ImageFilename);
	$DescriptionLink = mysqli_real_escape_string($conn, $item->DescriptionLink);

// SQL Query INSERT
	$sql = "INSERT INTO ProductsXMLTest (StockCode, CategoryCode, CategoryName, SubcategoryID, SubcategoryName, BarCode, Weight, ManufacturerSKU, ProductName, WarrantyLength, Manufacturer, DBP, DBP5, RRP, StandardRRP, AvailabilityTotal, AvailabilityAdelaide, AvailabilityBrisbane, AvailabilitySydney, AvailabilityMelbourne, AvailabilityPerth, ImageUrl, ImageFilename, DescriptionLink) VALUES ('$StockCode', '$CategoryCode', '$CategoryName', '$SubcategoryID', '$SubcategoryName', '$BarCode', '$Weight', '$ManufacturerSKU', '$ProductName', '$WarrantyLength', '$Manufacturer', '$DBP', '$DBP5', '$RRP', '$StandardRRP', '$AvailabilityTotal', '$AvailabilityAdelaide', '$AvailabilityBrisbane', '$AvailabilitySydney', '$AvailabilityMelbourne', '$AvailabilityPerth', '$ImageUrl', '$ImageFilename', '$DescriptionLink')";
	mysqli_query($conn, $sql) or die ("ERROR: " .mysqli_error($conn) . " (query was $sql)");
	}

// close connection
	mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:1)

未正确输入循环

foreach ($xml->channel->item as $item) {}

是正确的循环初始化

只是以为我会把解决方案搞砸,其他任何人都会犯这个错误