Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ Insert Multiple Rows into MySQL Database with PHP

Posted by: Dante Monaldo Aug 8 2012, 09:58 PM

I have some code that takes 50 products from an API catalog and should be adding them to a database on my site. However, when I run the code, 50 rows are created, but only one product is added. All the other rows are left blank.

Has anybody had this problem before? What am I missing?

CODE
$insert = mysql_query("INSERT INTO imported_products (advertiser_id, buy_url, catalog_id, currency, description, image_url, in_stock, isbn, manufacturer_name, manufacturer_sku, name, price, retail_price, sale_price, sku, upc)
                    VALUES('$advertiser_id', '$buy_url', '$catalog_id', '$currency', '$description', '$image_url', '$in_stock', '$isbn', '$manufacturer_name', '$manufacturer_sku', '$name', '$price', '$retail_price', '$sale_price', '$sku', '$upc')");
    if(!$insert){
        $update = mysql_query("UPDATE imported_products SET advertiser_id='$advertiser_id', buy_url='$buy_url', catalog_id='$catalog_id', currency='$currency', description='$description', image_url='$image_url', in_stock='$in_stock', isbn='$isbn', manufacturer_name='$manufacturer_name', manufacturer_sku='$manufacturer_sku', name='$name', price='$price', retail_price='$retail_price', sale_price='$sale_price', upc='$upc' WHERE sku='$sku'") or die (mysql_error());
        if(!$update){
            echo 'Could not update product. SKU: '.$sku.'';
        } else {
            echo 'UPDATE Mission Accomplished.';
        }
    } else {
        echo 'INSERT INTO Mission Accomplished.';
    }
}


I would really appreciate some help on this smile.gif

Posted by: Ephraim F. Moya Aug 9 2012, 08:55 AM

QUOTE(Dante Monaldo @ Aug 8 2012, 08:58 PM) *

I have some code that takes 50 products from an API catalog and should be adding them to a database on my site. However, when I run the code, 50 rows are created, but only one product is added. All the other rows are left blank.

Has anybody had this problem before? What am I missing?

CODE
$insert = mysql_query("INSERT INTO imported_products (advertiser_id, buy_url, catalog_id, currency, description, image_url, in_stock, isbn, manufacturer_name, manufacturer_sku, name, price, retail_price, sale_price, sku, upc)
                    VALUES('$advertiser_id', '$buy_url', '$catalog_id', '$currency', '$description', '$image_url', '$in_stock', '$isbn', '$manufacturer_name', '$manufacturer_sku', '$name', '$price', '$retail_price', '$sale_price', '$sku', '$upc')");
    if(!$insert){
        $update = mysql_query("UPDATE imported_products SET advertiser_id='$advertiser_id', buy_url='$buy_url', catalog_id='$catalog_id', currency='$currency', description='$description', image_url='$image_url', in_stock='$in_stock', isbn='$isbn', manufacturer_name='$manufacturer_name', manufacturer_sku='$manufacturer_sku', name='$name', price='$price', retail_price='$retail_price', sale_price='$sale_price', upc='$upc' WHERE sku='$sku'") or die (mysql_error());
        if(!$update){
            echo 'Could not update product. SKU: '.$sku.'';
        } else {
            echo 'UPDATE Mission Accomplished.';
        }
    } else {
        echo 'INSERT INTO Mission Accomplished.';
    }
}


I would really appreciate some help on this smile.gif


I don't see a loop.

You need a 'for' or a 'while' .

Posted by: Dante Monaldo Aug 9 2012, 12:42 PM

Whoops, looks like I left that chunk out. Here's the section of code that should be causing the problem.

CODE
foreach ($feed->products[0] as $entry) {
    $advertiser_id = $entry->$advertiser_id;
    $buy_url = $entry->$buy_url;
    $catalog_id = $entry->$catalog_id;
    $currency = $entry->$currency;
    $description = $entry->$description;
    $image_url = $entry->$image_url;
    $in_stock = $entry->$in_stock;
    $isbn = $entry->$isbn;
    $manufacturer_name = $entry->$manufacturer_name;
    $manufacturer_sku = $entry->$manufacturer_sku;
    $name = $entry->$name;
    $price = $entry->$price;
    $retail_price = $entry->$retail_price;
    $sale_price = $entry->$sale_price;
    $sku = $entry->$sku;
    $upc = $entry->$upc;

$insert = mysql_query("INSERT INTO imported_products (advertiser_id, buy_url, catalog_id, currency, description, image_url, in_stock, isbn, manufacturer_name, manufacturer_sku, name, price, retail_price, sale_price, sku, upc)
                    VALUES('$advertiser_id', '$buy_url', '$catalog_id', '$currency', '$description', '$image_url', '$in_stock', '$isbn', '$manufacturer_name', '$manufacturer_sku', '$name', '$price', '$retail_price', '$sale_price', '$sku', '$upc')");
    if(!$insert){
        $update = mysql_query("UPDATE imported_products SET advertiser_id='$advertiser_id', buy_url='$buy_url', catalog_id='$catalog_id', currency='$currency', description='$description', image_url='$image_url', in_stock='$in_stock', isbn='$isbn', manufacturer_name='$manufacturer_name', manufacturer_sku='$manufacturer_sku', name='$name', price='$price', retail_price='$retail_price', sale_price='$sale_price', upc='$upc' WHERE sku='$sku'") or die (mysql_error());
        if(!$update){
            echo 'Could not update product. SKU: '.$sku.'';
        } else {
            echo 'UPDATE Mission Accomplished.';
        }
    } else {
        echo 'INSERT INTO Mission Accomplished.';
    }
}

Posted by: Ephraim F. Moya Aug 9 2012, 03:46 PM

QUOTE(Dante Monaldo @ Aug 9 2012, 11:42 AM) *

Whoops, looks like I left that chunk out. Here's the section of code that should be causing the problem.

CODE
foreach ($feed->products[0] as $entry) {
    $advertiser_id = $entry->$advertiser_id;
    $buy_url = $entry->$buy_url;
    $catalog_id = $entry->$catalog_id;
    $currency = $entry->$currency;
    $description = $entry->$description;
    $image_url = $entry->$image_url;
    $in_stock = $entry->$in_stock;
    $isbn = $entry->$isbn;
    $manufacturer_name = $entry->$manufacturer_name;
    $manufacturer_sku = $entry->$manufacturer_sku;
    $name = $entry->$name;
    $price = $entry->$price;
    $retail_price = $entry->$retail_price;
    $sale_price = $entry->$sale_price;
    $sku = $entry->$sku;
    $upc = $entry->$upc;

$insert = mysql_query("INSERT INTO imported_products (advertiser_id, buy_url, catalog_id, currency, description, image_url, in_stock, isbn, manufacturer_name, manufacturer_sku, name, price, retail_price, sale_price, sku, upc)
                    VALUES('$advertiser_id', '$buy_url', '$catalog_id', '$currency', '$description', '$image_url', '$in_stock', '$isbn', '$manufacturer_name', '$manufacturer_sku', '$name', '$price', '$retail_price', '$sale_price', '$sku', '$upc')");
    if(!$insert){
        $update = mysql_query("UPDATE imported_products SET advertiser_id='$advertiser_id', buy_url='$buy_url', catalog_id='$catalog_id', currency='$currency', description='$description', image_url='$image_url', in_stock='$in_stock', isbn='$isbn', manufacturer_name='$manufacturer_name', manufacturer_sku='$manufacturer_sku', name='$name', price='$price', retail_price='$retail_price', sale_price='$sale_price', upc='$upc' WHERE sku='$sku'") or die (mysql_error());
        if(!$update){
            echo 'Could not update product. SKU: '.$sku.'';
        } else {
            echo 'UPDATE Mission Accomplished.';
        }
    } else {
        echo 'INSERT INTO Mission Accomplished.';
    }
}



Without knowing the rest of the code this looks suspicious:

$feed->products[0]

Should the '0' there be a variable?

Posted by: Dante Monaldo Aug 9 2012, 05:24 PM

I had this code working earlier to simply display the products on a page rather than enter them in a database, and it was working fine, even with the $feed->products[0].

The problem is that when I remove the variable [0] and run the code, it adds one row to the table, but the row is empty. Similarly, if the [0] is there, I get one row filled and 49 blank rows. (There should be 50 rows added every time I run it).

Here's a link to the page where I am simply displaying the products on screen rather than adding them to a database. http://bit.ly/NbSpuN

The code I have shown you is all that you need. The remaining code is irrelevant.

I really appreciate all the help!

Posted by: Ephraim F. Moya Aug 9 2012, 07:59 PM

QUOTE(Dante Monaldo @ Aug 9 2012, 04:24 PM) *

I had this code working earlier to simply display the products on a page rather than enter them in a database, and it was working fine, even with the $feed->products[0].

The problem is that when I remove the variable [0] and run the code, it adds one row to the table, but the row is empty. Similarly, if the [0] is there, I get one row filled and 49 blank rows. (There should be 50 rows added every time I run it).

Here's a link to the page where I am simply displaying the products on screen rather than adding them to a database. http://bit.ly/NbSpuN

The code I have shown you is all that you need. The remaining code is irrelevant.

I really appreciate all the help!


Then you did it wrong both times and it worked somewhat you like 50% of the time

The instruction 'foreach' is intended to iterate over an ARRAY! That's why I asked for more of a view into your data structure.

You are misusing the foreach instruction.

Yes, there's an added cycle that adds an empty output if your code does not correct for it.

First you need an array. Next each entry in that array must have a key.

Then the foreach instruction has the form:

foreach( $arrayname as $key => $value )
{
operate on each entry;
}
cancel last entry();

Posted by: Dante Monaldo Aug 9 2012, 11:08 PM

Yes, I think I hear what you are saying. I'm just confused how I should be using the $key => $value when there's multiple values that are related to each product (i.e. name, description, price, etc.)

You've been requesting the full code. Not sure how much help it's going to be, but here it is.

CODE
<?php
$webid="*******";
$CJ_ID="*******";
$url="https://product-search.api.cj.com/v2/product-search?website-id=$webid&advertiser-ids=joined";

// Define Variables
$advertiser_id = 'advertiser-id';
$buy_url = 'buy-url';
$catalog_id = 'catalog-id';
$currency = 'currency';
$description = 'description';
$image_url = 'image-url';
$in_stock = 'in-stock';
$isbn = 'isbn';
$manufacturer_name = 'manufacturer-name';
$manufacturer_sku = 'manufacturer-sku';
$name = 'name';
$price = 'price';
$retail_price = 'retail-price';
$sale_price = 'sale-price';
$sku = 'sku';
$upc = 'upc';

// Verify Headers
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, FAlSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_ID));
$xml = curl_exec($ch);
curl_close($ch);

function produce_XML_object_tree($raw_XML) {
    libxml_use_internal_errors(true);
    try {
        $xmlTree = new SimpleXMLElement($raw_XML);
    }
    catch (Exception $e) {
        $error_message = 'SimpleXMLElement threw an exception.';
        foreach(libxml_get_errors() as $error_line) {
            $error_message .= "\t" . $error_line->message;
            }
        trigger_error($error_message);
        return false;
    }
return $xmlTree;
}
$feed = produce_XML_object_tree($xml);

foreach ($feed->products as $entry) {
    $advertiser_id = $entry->$advertiser_id;
    $buy_url = $entry->$buy_url;
    $catalog_id = $entry->$catalog_id;
    $currency = $entry->$currency;
    $description = $entry->$description;
    $image_url = $entry->$image_url;
    $in_stock = $entry->$in_stock;
    $isbn = $entry->$isbn;
    $manufacturer_name = $entry->$manufacturer_name;
    $manufacturer_sku = $entry->$manufacturer_sku;
    $name = $entry->$name;
    $price = $entry->$price;
    $retail_price = $entry->$retail_price;
    $sale_price = $entry->$sale_price;
    $sku = $entry->$sku;
    $upc = $entry->$upc;

$insert = mysql_query("INSERT INTO imported_products (advertiser_id, buy_url, catalog_id, currency, description, image_url, in_stock, isbn, manufacturer_name, manufacturer_sku, name, price, retail_price, sale_price, sku, upc)
                    VALUES('$advertiser_id', '$buy_url', '$catalog_id', '$currency', '$description', '$image_url', '$in_stock', '$isbn', '$manufacturer_name', '$manufacturer_sku', '$name', '$price', '$retail_price', '$sale_price', '$sku', '$upc')");
    if(!$insert){
        $update = mysql_query("UPDATE imported_products SET advertiser_id='$advertiser_id', buy_url='$buy_url', catalog_id='$catalog_id', currency='$currency', description='$description', image_url='$image_url', in_stock='$in_stock', isbn='$isbn', manufacturer_name='$manufacturer_name', manufacturer_sku='$manufacturer_sku', name='$name', price='$price', retail_price='$retail_price', sale_price='$sale_price', upc='$upc' WHERE sku='$sku'") or die (mysql_error());
        if(!$update){
            echo 'Could not update product. SKU: '.$sku.'';
        } else {
            echo 'UPDATE Mission Accomplished.';
        }
    } else {
        echo 'INSERT INTO Mission Accomplished.';
    }
}
?>

Posted by: Ephraim F. Moya Aug 11 2012, 02:16 PM



CODE
This is what your array should look like:

$key[first] ----- $value['id']
            |---- $value['firstParam']
            |---- $value['...']
            |---- $value['lastParam']
  .
  .
  .
$key[last] ----- $value['id']
           |---- $value['firstParam']
           |---- $value['...']
           |---- $value['lastParam']

I usually use a special field in the array as the key. Mostly I use numeric ids because it is easier for the computer to distinguish the keys that way. Often there are missing keys in the id field. You code MUST be able to handle these. Also, a foreach construct will 'overshoot' by one field so you must take care of that, too.



Posted by: Brian Chandler Aug 11 2012, 10:36 PM

QUOTE
Also, a foreach construct will 'overshoot' by one field so you must take care of that, too.


Can you explain what this means? In general, one of the great conveniences of 'foreach' is that it *doesn't* "overshoot", that is, you don't have to be careful specifying the stopping limit.

Posted by: Ephraim F. Moya Aug 12 2012, 10:28 AM

QUOTE(Brian Chandler @ Aug 11 2012, 09:36 PM) *

QUOTE
Also, a foreach construct will 'overshoot' by one field so you must take care of that, too.


Can you explain what this means? In general, one of the great conveniences of 'foreach' is that it *doesn't* "overshoot", that is, you don't have to be careful specifying the stopping limit.


It's because the foreach construct leaves the status of the last cycle + 1 available after the loop is over. To a naive programmer this can look like another entry is available. This is a VERY common problem.

Google this: 'php foreach last iteration'


Posted by: Brian Chandler Aug 13 2012, 04:10 AM

QUOTE
It's because the foreach construct leaves the status of the last cycle + 1 available after the loop is over. To a naive programmer this can look like another entry is available. This is a VERY common problem.


I still can't parse "leaves the status of the last cycle + 1 available"... but I think you mean that after falling out of the loop, $value still has the last value from the array (and I suppose $key is still the index of the last value). Well, PHP is a classic of bad language design, and doesn't do variable scope properly at all. But unless you habitually refer to $value after it ought to have ceased to have any meaning (and I can't imagine any case where you would want to do this), I still can't see what the "Problem" is. In particular I can't imagine what you need to do to "take care of the overshoot". Can you give an example?

The problems with the OP's stuff seem to be more basic. What on earth is the point of the first bit (" Define Variables ") -- it actually just sets each of the variables to the string being its own name; later they get set to something meaningful, so this part could be deleted for a start. ... Oh, no, wait!

Instead of writing

CODE

$description = $entry->'description';


you write


CODE

$description = 'description';
$description = $entry->$description;


Wow! That is convoluted...

Incidentally, this is exactly where you should use a foreach. Define an array fields of the various fields in the structure:

CODE

$fields = array('
    'advertiser_id',
    'buy_url',
    'catalog_id',
    'currency',
    'description',
    'image_url',
    'in_stock',
    'isbn',
    'manufacturer_name',
    'manufacturer_sku',
    'name',
    'price',
    'retail_price',
    'sale_price',
    'sku',
    'upc');

// Now to do anything to every field...
// e.g. what the above achieves of copying every member out of the structure

foreach ($fields as $fld)
  $$fld = $entry -> $fld;


HTH

Posted by: Dante Monaldo Aug 13 2012, 03:45 PM

Okay Brian, I see what you are saying with putting the fields in an array, it would definitely simplify the code. But what about getting the actual data inserted into the database? I figured out the format of the feed and can control it as such:

$feed->products is the array the holds the multiple products

$feed->products->product is the array for the individual product that holds the data for that product

So now I have full control of displaying the products on the screen. For example, if I wanted to display the name of the 3rd product in the array, I would write:

CODE
echo $feed->products->product[3]->name;


And that works perfectly! I just can't get it into the database. When I run it, I can get one row added to the table with the product data in it, but that's it. It's as if the table will only hold one row. When I run it again, with a different product, nothing happens. Even when I do something simple like this, it still doesn't work:

CODE
$name = $feed->products->product[3]->name;
$insert = mysql_query("INSERT INTO imported_products (name) VALUES ('$name')");


Any ideas why it's doing this?

Posted by: Ephraim F. Moya Aug 13 2012, 10:27 PM

QUOTE(Brian Chandler @ Aug 13 2012, 03:10 AM) *


CODE

$fields = array('
    'advertiser_id',
    'buy_url',
    'catalog_id',
    'currency',
    'description',
    'image_url',
    'in_stock',
    'isbn',
    'manufacturer_name',
    'manufacturer_sku',
    'name',
    'price',
    'retail_price',
    'sale_price',
    'sku',
    'upc');

// Now to do anything to every field...
// e.g. what the above achieves of copying every member out of the structure

foreach ($fields as $fld)
  $$fld = $entry -> $fld;


HTH



This is in error!
1) there's an extra apostrophe in the definition.
2) This describes a single entry in a larger array.
3) Even though you don't use the key, there is still one. It starts at 0 and extends to the end of the array of arrays.

CODE
[0]---|--->'advertiser_id'
      |--->'more fields'
      |--->'upc'

more entries

[49]---|--->'advertiser_id'
       |--->'more fields'
       |--->'upc'

What Brian has described is one entry. He also forgot to put in the value. Each entry should look like this:

$fields = array(
'advertiser_id' => $idNumber,
'buy_url' => $url,
etc.,
);

So if your array is named $products then each element can be referenced by:

$upc = $products[key]['upc'];


Posted by: Brian Chandler Aug 14 2012, 01:37 AM

QUOTE
Okay Brian, I see what you are saying with putting the fields in an array, it would definitely simplify the code. But what about getting the actual data inserted into the database? I figured out the format of the feed and can control it as such:

$feed->products is the array the holds the multiple products

$feed->products->product is the array for the individual product that holds the data for that product


This does not make sense. If $feed->products is an array of products, then you access one particular product (supposing this array is just indexed from 0 to n-1 for some n) as $feed->products[17] (for example). And if you want to loop through the products, then the simplest way is:

CODE

foreach ( $feed->products  as $product)
{  <... do stuff with $product ...>
}


This is assuming your initial statement is correct about $feed->products. You might try using print_r() to dump $feed, then you *know* what you are talking about instead of guessing.


QUOTE
Any ideas why it's doing this?


Not immediately. But I am quite sure "it" is doing exactly what you are telling it to do. Get in the habit of using print_r()...

Incidentally, there are various legal SQL syntax variations, but the one you are using lists all of the fields, then lists all of the values. This is bound to be error prone, if these two lists do not line up. Much safer to use the clearer format as in (real example of mine):

CODE

"INSERT topic SET pcode = '$pcode', topiccode='$val'"


You can check this easily, because it looks like a set of assignments.

HTH

Posted by: Brian Chandler Aug 14 2012, 01:47 AM

QUOTE
This is in error!
1) there's an extra apostrophe in the definition.


Right. This is a typo...

QUOTE

2) This describes a single entry in a larger array.


No, it doesn't. It is an array of the field names. By having an array of the fieldnames you can do things to every field by looping through this array.

QUOTE

3) Even though you don't use the key, there is still one. It starts at 0 and extends to the end of the array of arrays.


Yes, of course. By default arrays are just like C arrays. (Don't really see the relevance of this bit.)

QUOTE
What Brian has described is one entry. He also forgot to put in the value. ...


You seem to have totally missed the point. (As above) No, I didn't forget anything, and this is not one "entry" of anything.

One problem is that PHP's arrays can be used as primitive structures (objects), using the array keys to identify which member of the structure you mean. But the two are really distinct (but it would be exactly in PHP's style to muddle them up by allowing the array notation ($thing[$member]) to access what should really be $thing->$member.

QUOTE

Each entry should look like this:$fields = array('advertiser_id' => $idNumber,'buy_url' => $url,etc.,);So if your array is named $products then each element can be referenced by:$upc = $products[key]['upc'];


But not if the "entry" is in fact a structure/object, not an array.

Posted by: Brian Chandler Aug 14 2012, 01:48 AM

QUOTE(Dante Monaldo @ Aug 14 2012, 05:45 AM) *

Okay Brian, I see what you are saying with putting the fields in an array, it would definitely simplify the code. But what about getting the actual data inserted into the database? I figured out the format of the feed...


But why did you need to "figure out" the format?? Is there no documentation for it?

Posted by: Ephraim F. Moya Aug 14 2012, 09:33 AM

QUOTE

But not if the "entry" is in fact a structure/object, not an array.


What about when each entry in the products array has a different structure? A php easy and valuable thing to do. Do you then use a structure/object for every entry? Or do you use another structure/object to describe what kind of structure each individual key's value has? Oh wait, the structure of each entry can dynamically change. Think of joining two (or more) individual arrays. Then you can add another level of keys sort of randomly. This can easily happen in an xml described data structure which this op seems to use.

Darn!



Posted by: Ephraim F. Moya Aug 16 2012, 12:07 PM

QUOTE(Dante Monaldo @ Aug 13 2012, 02:45 PM) *

Okay Brian, I see what you are saying with putting the fields in an array, it would definitely simplify the code. But what about getting the actual data inserted into the database? I figured out the format of the feed and can control it as such:

$feed->products is the array the holds the multiple products

$feed->products->product is the array for the individual product that holds the data for that product

So now I have full control of displaying the products on the screen. For example, if I wanted to display the name of the 3rd product in the array, I would write:
CODE
echo $feed->products->product[3]->name;


And that works perfectly! I just can't get it into the database. When I run it, I can get one row added to the table with the product data in it, but that's it. It's as if the table will only hold one row. When I run it again, with a different product, nothing happens. Even when I do something simple like this, it still doesn't work:

CODE
$name = $feed->products->product[3]->name;
$insert = mysql_query("INSERT INTO imported_products (name) VALUES ('$name')");


Any ideas why it's doing this?




Just after this point in your code:

$feed = produce_XML_object_tree($xml);

add these lines:

$f = print_r($feed, true);
echo "<pre>{$f}</pre>";
exit;

This should print out your entire $feed array.
Analyze the data you get to see exactly what you're getting. Look for the key format and the other field's formats. I believe you'll get something like I've posted before.


Posted by: Dante Monaldo Nov 11 2012, 08:45 PM

So, I'm back with another question relating to this. I used print_r and was able to work my way through most of the issues after that. Not sure why I couldn't get foreach to loop through and add each product to the database, so I used a count to add 1 to the product number after each entry into the database.

Here is the structure:
http://bit.ly/TUEzhs

The problem I have now is that whenever I try to add a product to the database, only the keys without a hyphen are added to the database. So for example, the key "name" is added, but not "buy-url". If the key has a hyphen in it, then a zero will be entered into the database rather than the value of the key. I'm thinking it's a simple syntax problem.

CODE

<?php
$webid="*****";
$CJ_ID="*****";
$url="https://product-search.api.cj.com/v2/product-search?website-id=$webid&advertiser-ids=joined&page-number=1";

// Verify Headers
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, FAlSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_ID));
$xml = curl_exec($ch);
curl_close($ch);

function produce_XML_object_tree($raw_XML) {
    libxml_use_internal_errors(true);
    try {
        $xmlTree = new SimpleXMLElement($raw_XML);
    }
    catch (Exception $e) {
        $error_message = 'SimpleXMLElement threw an exception.';
        foreach(libxml_get_errors() as $error_line) {
            $error_message .= "\t" . $error_line->message;
            }
        trigger_error($error_message);
        return false;
    }
return $xmlTree;
}
$feed = produce_XML_object_tree($xml);

$con = mysql_connect("localhost","root","Dan7ger5ous3!!");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("main", $con);

for($count=0; $count<=50; $count++){
    foreach ($feed->products as $key=>$value) {
        $advertiser_id = $value->product[$count]->advertiser-id;
        $buy_url = $value->product[$count]->buy-url;
        $catalog_id = $value->product[$count]->catalog-id;
        $currency = $value->product[$count]->currency;
        $description = $value->product[$count]->description;
        $image_url = $value->product[$count]->image-url;
        $in_stock = $value->product[$count]->in-stock;
        $isbn = $value->product[$count]->isbn;
        $manufacturer_name = $value->product[$count]->manufacturer-name;
        $manufacturer_sku = $value->product[$count]->manufacturer-sku;
        $name = $value->product[$count]->name;
        $price = $value->product[$count]->price;
        $retail_price = $value->product[$count]->retail-price;
        $sale_price = $value->product[$count]->sale-price;
        $sku = $value->product[$count]->sku;
        $upc = $value->product[$count]->upc;
        
$query = "INSERT INTO imported_products (advertiser_id, buy_url, catalog_id, currency, description, image_url, in_stock, isbn, manufacturer_name, manufacturer_sku, name, price, retail_price, sale_price, sku, upc)
        VALUES('$advertiser_id', '$buy_url', '$catalog_id', '$currency', '$description', '$image_url', '$in_stock', '$isbn', '$manufacturer_name', '$manufacturer_sku', '$name', '$price', '$retail_price', '$sale_price', '$sku', '$upc')
            ON DUPLICATE KEY UPDATE advertiser_id='$advertiser_id', buy_url='$buy_url', catalog_id='$catalog_id', currency='$currency', description='$description', image_url='$image_url', in_stock='$in_stock', isbn='$isbn', manufacturer_name='$manufacturer_name', manufacturer_sku='$manufacturer_sku', name='$name', price='$price', retail_price='$retail_price', sale_price='$sale_price', upc='$upc'" or die(mysql_error());
$insert = mysql_query($query);

        }
}
?>

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)