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 |
Brian Chandler |
Aug 14 2012, 01:47 AM
Post
#2
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
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. |
Lo-Fi Version | Time is now: 28th April 2024 - 07:56 AM |