Search code examples
phparraysexplodeimplode

how to use two arrays or three arrays in the foreach loop in php for insert to database


How to insert multiple arrays with a foreach in rows of databases?

It's not a problem with an array, but my problem is to use two arrays or more.

my php code:

$checkBox1 = implode(',', $_POST['goinput1']);
$checkBox2 = implode(',', $_POST['goinput3']);
$mark3=explode(',', $checkBox1);
$mark4=explode(',', $checkBox2);
foreach($mark3 as $out3) 
{
    $sql_sub = "INSERT INTO sub_forms
                        (bord_mizban,bord_mihman) 
                VALUES ('$out3','$out4')";      
    if ($conn->query($sql_sub) === TRUE) {  

    } else {

    }   

}

** I want the out 4 variable insert to the database with the out 3 variable **


Solution

  • Iterate over $mark3 using indexes ($key) and get element under same key:

    foreach($mark3 as $key => $out3) 
    {
        echo $out3 . '; ' . $mark4[$key];
    }