How to insert data into a mysql table from an array using php?
-
am new to php and need help regarding array insertion into mysql table i have an array when i add and item to the shopping cart $_session['cart_array'] = array("item_id" => $pid, "quantity"=>1) now if i have one item in cart when i do print_r($_session['cart_array']) array looks like Array( [0] => Array ( [item_id] => 1 [quantity] => 1 ) ) now i need to insert this array data to a table named purchased_products having columns id, product_name, product_quantity please help me out its urgent thanks php mysql
-
Answer:
if it were me, I would loop through the array and build the query, then execute it once. Like this $output = 'insert into table(item_id,pid,quantity) values'; foreach($array_name as $row){ $output .= " ('{$row['item_id']}', '{$orw['pid']}', {$row['quantity']}),"; } //then you need to correct for the extra comma $output = substr($output,0,strlen($output)-1); //then query mysql_query($output); Now I haven't checked this, but it should be close to what you need. Good luck.
Ahmad at Yahoo! Answers Visit the source
Other answers
if it were me, I would loop through the array and build the query, then execute it once. Like this $output = 'insert into table(item_id,pid,quantity) values'; foreach($array_name as $row){ $output .= " ('{$row['item_id']}', '{$orw['pid']}', {$row['quantity']}),"; } //then you need to correct for the extra comma $output = substr($output,0,strlen($output)-1); //then query mysql_query($output); Now I haven't checked this, but it should be close to what you need. Good luck.
Related Q & A:
- How to insert data in db using jsp?Best solution by Yahoo! Answers
- How to insert data from one table to another?Best solution by Stack Overflow
- how to insert multiple rows from MySQL with checkbox?Best solution by Stack Overflow
- How to scrape data from a website?Best solution by Stack Overflow
- How to insert data into database in electron?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.