Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert 2 array value to mysql db

Tags:

php

I'm getting 2 fields from my html form which store the value in an Array.

$ingredients = $_POST['ingredients'];
$quantity = $_POST['quantity'];

I want to insert these 2 value to my mysql db. So I'm using following:

foreach($ingredients  as $in)
{
    foreach($quantity as $q)
    {
        echo "Intredent and quantity is : $in and $q<br/>"; 

        //$insert = my mysql Insert query;
    }
}

But it showing twice value. For ex: if it's 2 value it's showing 4 value.. etc.

like image 748
Babu Avatar asked Jan 12 '23 09:01

Babu


2 Answers

foreach($ingredients as $key => $in)
{
    echo "Intredent and quantity is : $in and $_POST['quantity'][$key]<br/>";
}

Are you trying to do that? Each ingredient with the quantity amount?

like image 116
Nikola Avatar answered Jan 16 '23 20:01

Nikola


try this

$ingredients = $_POST['ingredients'];
$quantity = $_POST['quantity'];

$arr_count = sizeof($ingredients);

for($i=0; $i<$arr_count; $i++)
{
    $in = $ingredients[$i];
    $q = $quantity[$i];

      echo "Intredent and quantity is : $in and $q<br/>"; 

       //$insert = my mysql Insert query;
}
like image 41
Satish Sharma Avatar answered Jan 16 '23 22:01

Satish Sharma