
Originally Posted by
DrByte
What if you replace the "mysql_query" call with just an "echo" call? ie: output the generated SQL to the screen, to see what it's really doing?
Then what happens if you run that SQL in phpMyAdmin? What happens?
Both of those steps can be very revealing of where you've got logic or syntax errors.
the following is my php code, granted im a novice and its been peiced together form examples off the net. perhaps you could validate?
PHP Code:
<?php
$con = mysql_connect('localhost', '<username>', '<password>);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('overplan_mirror');
$rssFeeds = array ('supplier.xml');
//Loop through the array, reading the feeds one by one
foreach ($rssFeeds as $feed) {
readFeeds($feed);
}
function startElement($xp,$name,$attributes) {
global $item,$currentElement; $currentElement = $name;
//the other functions will always know which element we're parsing
if ($currentElement == 'PRODUCT') {
//by default PHP converts everything to uppercase
$item = true;
// We're only interested in the contents of the item element.
////This flag keeps track of where we are
}}
function endElement($xp,$name) {
global $item,$currentElement,$productcode,$stock,$duedate;$conn;
if ($name == 'PRODUCT') {
// If we're at the end of the item element, display
// the data, and reset the globals
echo "<b>Product Code:</b> $productcode<br>";
echo "<b>Stock:</b> $stock<br>";
echo "<b>Due Date:</b> $duedate<br><br>";
$ins_product = addslashes($productcode);
$ins_qty = addslashes($stock);
$ins_due = addslashes($duedate);
mysql_query("UPDATE products_with_attributes_stock SET quantity = $stock WHERE product_code = $productcode");
$productcode = '';
$stock = '';
$duedate = '';
$item = false; }}
function characterDataHandler($xp,$data) {
global $item,$currentElement,$productcode,$stock,$duedate;
if ($item) {
//Only add to the globals if we're inside an item element.
switch($currentElement) {
case "PRODUCT_CODE":
$productcode .= $data;
// We use .= because this function may be called multiple
// times for one element.
break;
case "AVAILABLE_STOCK":
$stock.=$data;
break;
case "DUE_DATE":
$duedate.=$data;
break; } }}
function readFeeds($feed) {
$fh = fopen($feed,'r');
// open file for reading
$xp = xml_parser_create();
// Create an XML parser resource
xml_set_element_handler($xp, "startElement", "endElement");
// defines which functions to call when element started/ended
xml_set_character_data_handler($xp, "characterDataHandler");
while ($data = fread($fh, 4096)) {
if (!xml_parse($xp,$data)) {
return 'Error in the feed';
}
}
}
?>
Also supplier.xml is in the following format:
<StockValues>
−
<Product>
<Product_Code>00380 </Product_Code>
<Available_Stock>3960</Available_Stock>
<Due_Date>N/A</Due_Date>
</Product>
−
<Product>
<Product_Code>00429 </Product_Code>
<Available_Stock>4285</Available_Stock>
<Due_Date>N/A</Due_Date>
</Product>
−
<Product>
<Product_Code>00495 </Product_Code>
<Available_Stock>1035</Available_Stock>
<Due_Date>N/A</Due_Date>
</Product>
−
<Product>
</StockValues>
********The Following is the php output echo from the code above as it is******
mysql_query("UPDATE products_with_attributes_stock SET quantity = $stock WHERE smiffy_id = $productcode");
Product Code: 00380
Stock: 3960
Due Date: N/A
Product Code: 00429
Stock: 4285
Due Date: N/A
Product Code: 00495
Stock: 1035
Due Date: N/A
Product Code: 0122
Stock: 2953
Due Date: N/A
Product Code: 0190
Stock: 3434
Due Date: N/A
Product Code: 052A
Stock: 550
Due Date: N/A
*********************When I echo the queery**************************:
echo = ("UPDATE products_with_attributes_stock SET quantity = $stock WHERE smiffy_id = $productcode");
UPDATE products_with_attributes_stock SET quantity = 789 WHERE product_code = 20320 Product Code: 20389L
Stock: 11
Due Date: N/A
UPDATE products_with_attributes_stock SET quantity = 11 WHERE product_code = 20389L Product Code: 20389M
Stock: 161
Due Date: N/A
UPDATE products_with_attributes_stock SET quantity = 161 WHERE product_code = 20389M Product Code: 20390M
Stock: 87
Due Date: N/A
UPDATE products_with_attributes_stock SET quantity = 87 WHERE product_code = 20390M Product Code: 20390S
Stock: 0
Due Date: N/A
UPDATE products_with_attributes_stock SET quantity = 0 WHERE product_code = 20390S Product Code: 20392L
Stock: 160
Due Date: N/A
Im just about to echo the code you sent to see what that shows..