Insert more than one array using one query

I want to add an array of flags, drop-down lists and dates to the database. If I checked all the checkboxes everything works fine. However, when I checked certain flags, the value of the flags can be inserted, but not the value of the drop-down list and date.

This is the code for the checkbox, drop down menu and date:

<div class='field'>
    <div class='checkboxes'>
        <div class='checkbox'>
            <input type='checkbox' id='spesimen$i' name='spesimen[]' value='$JenisSpesimen' required minlength='1'/><label>$JenisSpesimen</label><br>
        </div>
        <div class='select'>
            <select id='bilangan$i' name='bilangan[]' class='med' style='display: none;'>
                <option></option>
                <option value='Pertama'>Pertama</option>
                <option value='Kedua'>Kedua</option>
            </select>
        </div>
        <br>
        <div class='input' id='tarikh_ambil$i' style='display: none;'>
            <input type='text' id='tarikh_ambil_spesimen$i' name='tarikh_ambil_spesimen[]' class='small' readonly/>
        </div>
    </div>
</div>

And this process:

$spesimen = $_POST['spesimen'];         
$countSpesimen = count($_POST['spesimen']);

$bilangan = $_POST['bilangan'];
//$countBilagan = count($_POST['bilangan']);

$tarikh_ambil = $_POST['tarikh_ambil_spesimen'];    
//$countTarikh = count($_POST['tarikh_ambil_spesimen']);

for ( $x = 0; $x < $countSpesimen; $x++)
    {
        $xx = $x+1;
        $SubIDMohon = $IDMohonx.'-'.$xx;

        $dd=substr($tarikh_ambil[$x], 0, 2);
        $mm=substr($tarikh_ambil[$x], 3, 2);
        $yy=substr($tarikh_ambil[$x], 6, 4);
        $tarikh_ambil[$x] = $yy."-".$mm."-".$dd;
        if($tarikh_ambil[$x] == '--') { $tarikh_ambil[$x] = '0000-00-00'; }

        $pdo->exec("insert into simka_spesimen(IDMohon,SubIDMohon, Nama, LainLain, TarikhAmbil, TarikhHantar, TarikhMakmalTerima)
            values ('".$IDMohonx."','".$SubIDMohon."','".$spesimen[$x]."','".$bilangan[$x]."','".$tarikh_ambil[$x]."','".$tarikh_hantar_spesimen."','".$tarikh_terima_spesimen."')");
    }
+4
source share
1 answer

I would recommend wrapping your fields in a common name so that you can run foreach on each "set of fields" and then access your respective fields, instead of having them separate and independent, as you now have.

HTML : ( , , )

  <div class='field'>
    <div class='checkboxes'>
        <div class='checkbox'>
            <label><input type='checkbox' id='spesimen$i' name='fieldset[$counter][spesimen]' value='$JenisSpesimen' required minlength='1'/>$JenisSpesimen</label>
        </div>
        <div class='select'>
            <select id='bilangan$i' name='fieldset[$counter][bilangan]' class='med' style='display:none;'>
                <option></option>
                <option value='Pertama'>Pertama</option>
                <option value='Kedua'>Kedua</option>
            </select>
        </div>
        <div class='input' id='tarikh_ambil$i' style='display:none;'>
            <input type='text' id='tarikh_ambil_spesimen$i' name='fieldset[$counter][tarikh_ambil_spesimen]' class='small' readonly/>
        </div>
    </div>
</div>

PHP :

foreach($_POST['fieldset'] as $i=>$fields){

    $SubIDMohon = $IDMohonx .'-' . ($i + 1) ;

    //If specimen is checked, the value comes with post, otherwise set it to default ''
    $specimen = ( isset($fields['specimen']) ) ? $fields['specimen'] : '';

    //check if the date is valid format
    $date = '';
    if(preg_match('^([0-9]{2}-){2}[0-9]{4}$', $fields['tarikh_ambil_spesimen'])){ 
        $date = implode('-', array_reverse( explode('-', $fields['tarikh_ambil_spesimen']) ) );
    }
    else{
        $date = '0000-00-00';
    }


    $pdo->exec(
        "insert into simka_spesimen(IDMohon,SubIDMohon, Nama, LainLain, TarikhAmbil, TarikhHantar, TarikhMakmalTerima)
            values (
                '" . $IDMohonx . "',
                '" . $SubIDMohon . "',
                '" . $spesimen . "',
                '" . $fields['bilangan'] . "',
                '" . $date . "',
                '" . $tarikh_hantar_spesimen . "',
                '" . $tarikh_terima_spesimen . "'
            )"
    );
}

print_r $_POST, , .

!

0

All Articles