I want to ask about array. How to prevent from empty array inserted into database?. I already use !empty
, but its still not working. If I move the
for ($i = 0; $i < count($spm); $i++)
before if(!empty)
it have the error. Please help.
<?php
if(!empty($spm)&&$total_spm==0)
{
for ($i = 0; $i < count($spm); $i++) {
$spmSubjek =($spm[$i]);
$gredSubejek = ($gredSPM[$i]);
$sqlInsertSpm=OCIParse($c,$a="INSERT INTO E_PENDIDIKAN_PELAJAR
(
DIDIK_NOMKPB,
DIDIK_KOD_PERIKSA,
DIDIK_GRED,
DIDIK_NAMA,
DIDIK_KTRGN,
DIDIK_THN_DARI,
DIDIK_TRKH_UPD,
DIDIK_CREATED_USER,
DIDIK_CREATED_DATE,
DIDIK_JENIS_SEKOLAH
)
VALUES
(
'$nomkpb',
'0',
'$gredSubejek',
'$spmSubjek',
'$spmSubjek',
'$year_spm_bm',
to_date('".$curr_date."','dd-mm-rrrr hh:mi AM'),
'IPSWEB',
to_date('".$curr_date."','dd-mm-rrrr hh:mi AM'),
'$jenis_sekolah'
)
");
OCIExecute($sqlInsertSpm);
OCICommit($c);
}}
?>
Use foreach
instead of that for
loop. foreach
will execute the inner code zero times if the array is empty, ie you do not have to prevent anything.
foreach ($spm as $i => $spmSubjek) {
// not needed, it is done by foreach for you $spmSubjek =($spm[$i]);
$gredSubejek = ($gredSPM[$i]); // still needed, but $i is taken from foreach
...
}
More information is in the documentation: http://php.net/manual/en/control-structures.foreach.php
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.