Hi all,

I have an array:

<?php

     $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 
         break;
         
         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. 
         break;
         
         case "empty":
             unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
         break;
     
     }
     
?>

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')"; 
   mysql_query($query);
}

I think this should be enough.
Cheers,
Nav

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)
value
('$valname','$valtd','$valprob')";
}
}
}

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

@wayko

$array = array(
            0=>array('fname'=>'john','sname'=>'smith','age'=>32)
            ,1=>array('fname'=>'Bill','sname'=>'Bailey','age'=>22)
            ,2=>array('fname'=>'Fred','sname'=>'Kruger','age'=>0)
            );
foreach($array as $v){
    $sql = "INSERT INTO people (fname,sname,age)
value
('{$v['fname']}','{$v['sname']}',{$v['age']})";
    //execute $sql here or it will overwrite on loop
    mysql_query($sql);
}

any one having complate tutorial ..

Member Avatar for diafol

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, networking, learning, and sharing knowledge.