0

I have small problem here :)

Anyone can help me, what I should do if i want to use this foreach statement with 2 values, because I need to insert in one table at the same time. Because if I used this code, it will be error. So && is not properly used in this case?

$hasilmsisdn=explode(";", $msisdn);
$hasilaccid=explode(";", $accid);
     foreach ($hasilmsisdn as $value "&&" $hasilaccid as $value2)
    {
        $sql = "INSERT INTO complaint_detail (MSISDN,ACCID) VALUES ('$value','$value2');";
        $run=mysql_query($sql);
    }

Thanks all, hope you help me :)

chris85
  • 23,846
  • 7
  • 34
  • 51
Delinda Dyta
  • 187
  • 1
  • 2
  • 12

2 Answers2

3

Use array_combine() to combine both arrays as key/value pairs:

$hasilmsisdn=explode(";", $msisdn);
$hasilaccid=explode(";", $accid);
$array = array_combine($hasilmsisdn,$hasilaccid);
foreach ($array as $msisdn => $accid){
    $sql = "INSERT INTO complaint_detail (MSISDN,ACCID) VALUES ('$msisdn','$accid');";
    $run=mysql_query($sql);
}

While you're at it you should definitely consider switching over to PDO or mysqli

ElefantPhace
  • 3,806
  • 3
  • 20
  • 36
1

If both the arrays are having the same size you can use a for loop like this:

for($i = 0, $i < count($hasilmsisdn); $i++) {
     $sql = "INSERT INTO complaint_detail (MSISDN, ACCID) VALUES ('$hasilmsisdn[$i]', '$hasilaccid[$i]');";
     $run = mysql_query($sql);
}
AkiEru
  • 780
  • 1
  • 9
  • 34