So guys i have this array that i need to insert into my db. I'm using foreach but i can't get it to run with two values.
The simple array looks like this:
Array
(
[1] => Array
(
[id_produs] => 'value'
[id_pret] => 'value'
)
[2] => Array
(
[id_produs] => 'value'
[id_pret] => 'value'
)
.
.
.
[i] => Array
(
[id_produs] => 'value'
[id_pret] => 'value'
)
)
This is what I got right now:
foreach ($vl['id_produs'] as $prds=>$val)
{
if($val<>'') mysql_query("INSERT into db set id_pachet='".$id_pachet."', id_produs='".$val."'");
foreach ($vl['id_pret'] as $prdsv=>$valv) {
if($valv<>'') mysql_query("INSERT into db set id_pachet='".$id_pachet."', id_pret='".$valv."'");
}
}
where:
$vl = array();
$vl = $_POST;
What do I do wrong?
The hole array looks like this:
Array
(
[nume_pachet] => Test pachet nou
[id_produs] => Array
(
[0] => 0
[1] => 41
[2] => 135
)
[id_pret] => Array
(
[0] => 0
[1] => 0
[2] => 157
)
[pret_pachet] => 99.00
[id_moneda] => 1
[descriere_pachet] => Lorem ipsum dolores
[activ] => 1
)
The db in which I want to insert the values has this structure:
id id_pachet id_produs id_pret
so In one package (id_pachet) I can have more products (id_produs) which can have more than one price (id_pret)
for example:
id id_pachet id_produs id_pret
1 3 13 1
2 3 13 2
3 3 14 0
The thing is that if do this:
foreach ($vl['id_produs'] as $prds=>$val)
{
if($val<>'') mysql_query("INSERT into erad_produse_pachete_str set id_pachet='".$id_pachet."', id_produs='".$val."'");
}
it's working for id_produs, but i need to insert at the same time also id_pret
UPDATE!
I got some help from my brother. And this is working for me. If you can apply it for your case I hope it helps
$produse = $_POST['id_produs'];
$preturi = $_POST['id_pret'];
if(count($produse))
foreach ($produse as $key=>$id_produs) {
if($id_produs>0){
$id_pret = isset($preturi[$key]) ? $preturi[$key] : 0;
mysql_query("INSERT INTO erad_produse_pachete_str (id_pachet,id_produs,id_pret) VALUES ('{$id_pachet}', '{$id_produs}', '{$id_pret}')");
}
}