Hi all,

I have an array:


     $product_id = $_GET[id];     //the product id from the URL 
     $action     = $_GET[action]; //the action from the URL 

     //if there is an product_id and that product_id doesn't exist display an error message
     if($product_id && !productExists($product_id)) {
         die("Error. Product Doesn't Exist");

     switch($action) {    //decide what to do    
         case "add":
             $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id 
         case "remove":
             $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id 
             if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
         case "empty":
             unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 

How can i add the data from the array to a Mysql table?

Recommended Answers

All 6 Replies

How can i add the data from the array to a Mysql table?

If you have a multidimensional array (or even a normal array), you should go through each and every element of the array (iterate it) and then insert the value to the table. For example,

$array = array("a","b","c","d"); //an array with 4 elements which you want to insert to the table
foreach ($array as $value) { //for each element of the array insert the value
  $query = "insert into table (col1) values ('$value')"; 

I think this should be enough.

what if I have to insert multiple columns

when I use the foreach() I get inserts multiple time for each variable
IE:(valname1,td1,valprob1)(valname1,td1,valprob2)ect ect...

foreach($name as $valname){
foreach($problem as $valprob){
foreach($timedate as $valtd){
$sql = "INSERT INTO roomchart (Name,TimeDate,Problem)

but if I don't format it that way I only get the last item of the array


$array = array(
foreach($array as $v){
    $sql = "INSERT INTO people (fname,sname,age)
    //execute $sql here or it will overwrite on loop

any one having complate tutorial ..

Member Avatar

any one having complate tutorial ..

I would imagine that your Google abilities are as good as ours. After all, the majority of us would have to search for a specific tutorial like this - or worse still write a solution where the poster has not made any effort himself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.