Nehil Mistry
Nehil Mistry

Reputation: 1109

Insert into table from Array PHP

I have four Array consisting of some values. I want to insert Array with same index in same row. Eg:

$product = [Facebook, Twitter]; 
$sub_product = [Likes, Boost]; 
$plan = [10k, 20k];
$months = [3,6]; 

I want table to be some thing like this

+----+----------+-------------+------+--------+
| id | product  | sub_product | plan | months |
+----+----------+-------------+------+--------+
| 1  | Facebook | Likes       | 10k  | 3      |
+----+----------+-------------+------+--------+
| 2  | Twitter  | Boost       | 20k  | 6      |
+----+----------+-------------+------+--------+

What will be the best way to achieve this such that Index of each array falls in same row? I want to insert in HTML table

Upvotes: 2

Views: 8227

Answers (6)

Priyank
Priyank

Reputation: 1009

Try This Code,

    foreach($product as $key=>$p){
    $data = array(
    'product'=>$p,
    'sub_product'=>$sub_product[$key],
    'plan'=>$plan[$key],
    'months'=>$months[$key]
    );

    //Code to insert this array to Database
    // Create connection
         $conn = mysqli_connect($servername, $username, $password, $dbname);
         $sql = "INSERT INTO `table`('product','sub_product',...) VALUES ($data['product'],$data['sub_product'],...)";
         mysqli_close($conn);

    //OR If you are using Codeigniter,
         $this->db->insert('table',$data);

    }

Upvotes: 5

Vegeta
Vegeta

Reputation: 1317

If you want to execute mysql_query in single shot then

$product = [Facebook, Twitter]; 
$sub_product = [Likes, Boost]; 
$plan = [10k, 20k];
$months = [3,6]; 
$query = 'insert into TABLE (product,sub_product,plan,months) values';
foreach( $product as $index => $col ){
    $query .= "( '".$product[$index]."', '".$sub_product[$index]."', '".$plan[$index]."', ".$months[$index]." ),";
}

$query = rtrim( $query, ',');
mysqli_query(mysql_query);

This will be faster than executing multiple mysql_query function in a loop.

Upvotes: 6

AkshayS
AkshayS

Reputation: 34

Assuming that the size of all the above arrays are same at a given point of time and their indexing is consistent you can use the following code:

    <?php

    $product = array('Facebook', 'Twitter'); 
    $sub_product = array('Likes', 'Boost'); 
    $plan = array('10k', '20k');
    $months = array(3,6); 

/* Connected to a mysql Database */

$i = 0; //initialize a counter

while($i < sizeof($product)) //Run the loop (Twice in this case)
{
//Store the current iteration value in variables
$current_product = $product[$i];
$current_sub_product = $sub_product[$i];
$current_plan = $plan[$i];
$current_months = $months[$i];



    //Prepare the SQL statement
$sql = <<<EOD
        INSERT INTO table_product(product,subproduct,plan,months) 
        VALUES ('$current_product','$current_sub_product','$current_plan',$current_months)  
EOD;

$i++;
echo $sql . "<br />";
}


?>

Upvotes: 0

wogsland
wogsland

Reputation: 9508

Assuming all arrays have the same length and $mysqli is a connection to your database,

for ($i=0; $i < count($product); $i++) {
    $query = "INSERT INTO your_table (id, product, sub_product, plan, months)";
    $query .= " VALUES ('$i', '{$product[$i]}', '{$sub_product[$i]}', '{$plan[$i]}', '{$months[$i]}')";
    $mysqli->query($query);
}

Upvotes: 0

Amit Rajput
Amit Rajput

Reputation: 2059

Do on this way simple:

$count = count($product);
$index=0;
while($index<$count){
//do your stuff here i.e. insert query
$sql = "INSERT INTO your_table SET product='".$product[$index]."', sub_product='".$sub_product[$index]."', plan='".$plan[$index]."', months='".$months[$index]."' ";
mysql_query($sql);
$index++;
}

Upvotes: -1

Amit Shah
Amit Shah

Reputation: 1380

this code should work,

$product = array("Facebook", "Twitter"); 
$sub_product = array("Likes", "Boost"); 
$plan = array("10k", "20k");
$months = array(3,6); 

for($i=0;$i<count($product);$i++){

    $product_name=$product[$i];
    $sub_product_name=$sub_product[$i];
    $plan_name=$plan[$i];
    $month_no=$months[$i];

    echo "insert into table_name (product_name, sub_product_name, plan_name, month_no) values ('$product_name', '$sub_product_name', '$plan_name', '$month_no')";
    echo "<br>";

}

Thanks Amit

Upvotes: 0

Related Questions