Insert Multiple Rows into MySQL Database with PHP, Why does it only insert one row? |
Insert Multiple Rows into MySQL Database with PHP, Why does it only insert one row? |
Dante Monaldo |
Aug 8 2012, 09:58 PM
Post
#1
|
Advanced Member Group: Members Posts: 124 Joined: 22-March 09 From: California, USA Member No.: 8,132 |
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 |
Dante Monaldo |
Aug 13 2012, 03:45 PM
Post
#2
|
Advanced Member Group: Members Posts: 124 Joined: 22-March 09 From: California, USA Member No.: 8,132 |
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? |
Brian Chandler |
Aug 14 2012, 01:48 AM
Post
#3
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
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? |
Lo-Fi Version | Time is now: 27th April 2024 - 11:06 PM |