I have a lot of $_POST variables and want to insert them into the database.
it works. But I must set a default value of "NA" when a user doesn't fill in the form.
this my code:
$tgl_real = $_POST['TGL_REAL'];
$rid = $_POST['RID'];
$mesin = $_POST['MESIN'];
$shift = $_POST['SHIFT'];
$produk = $_POST['PRODUK'];
$tanggal = $_POST['HT'];
$no_aduk = $_POST['NO_ADUK'];
$sho = $_POST['SHO'];
$tb1 = $_POST['TB1'];
$gr1 = $_POST['GR1'];
$tb2 = $_POST['TB2'];
$gr2 = $_POST['GR2'];
$tb3 = $_POST['TB3'];
$gr3 = $_POST['GR3'];
$tb4 = $_POST['TB4'];
$gr4 = $_POST['GR4'];
$tb5 = $_POST['TB5'];
$gr5 = $_POST['GR5'];
$spr_besar = $_POST['SPR_BESAR'];
$spr_kecil = $_POST['SPR_KECIL'];
$br1 = $_POST['BR1'];
$br2 = $_POST['BR2'];
$br3 = $_POST['BR3'];
$tebal1 = $_POST['TEBAL1'];
$tebal2 = $_POST['TEBAL2'];
$tebal3 = $_POST['TEBAL3'];
$panjang = $_POST['PANJANG'];
$lebar = $_POST['LEBAR'];
$diameter = $_POST['DIAMETER'];
$kc = $_POST['KC'];
$kkc = $_POST['KKC'];
$kerataaan_poles = $_POST['KERATAAN_POLES'];
$sql = "INSERT INTO adonan SET `RID` = '$rid',
`MESIN` = '$mesin',
`SHIFT` = '$shift',
`HARI_TGL` = '$tanggal',
`TGL_REAL` = '$tgl_real',
`PRODUK` = '$produk',
`NO_ADUK` = '$no_aduk',
`KERATAAN_SHO` = '$sho',
`TEBAL_SHEET_GR1` = '$tb1',
`KECAPATAN_KAIN_GR1` = '$gr1',
`TEBAL_SHEET_GR2` = '$tb2',
`KECAPATAN_KAIN_GR2` = '$gr2',
`TEBAL_SHEET_GR3` = '$tb3',
`KECAPATAN_KAIN_GR3` = '$gr3',
`TEBAL_SHEET_GR4` = '$tb4',
`KECAPATAN_KAIN_GR4` = '$gr4',
`TEBAL_SHEET_GR5` = '$tb5',
`KECAPATAN_KAIN_GR5` = '$gr5',
`SPEED_ROLL_BESAR_WC` = '$spr_besar',
`SPEED_ROLL_KECIL_WC` = '$spr_kecil',
`BERAT1_ADONAN` = '$br1',
`BERAT2_ADONAN` = '$br2',
`BERAT3_ADONAN` = '$br3',
`KETEBALAN1_ADONAN` = '$tebal1',
`KETEBALAN2_ADONAN` = '$tebal2',
`KETEBALAN3_ADONAN` = '$tebal3',
`PANJANG_ADONAN` = '$panjang',
`LEBAR_ADONAN` = '$lebar',
`DIAMATER_ADONAN` = '$diameter',
`MOLDER_SPEED` = '$kc',
`KONVEYOR_SPEED` = '$kkc',
`KERATAAN_POLES` = '$kerataaan_poles',
`PROSENTASE_POLES` = '$prosentase_poles',
`KERATAAN_GULA` = '$kerataaan_gula',
`PROSENTASI_GULA` = '$kerataaan_gula'";
That $_POST is geting data from a file, but how do I set a default ("NA") value
in my database when a user doesn't fill in a value.
Nb:
I have tried to set a default NA in my mysql database, but it didn't work when a user doesn't fill in, my column field is still empty
Dont advice me to use jquery validation, because sometimes a user fills it in and somtimes not.
The database column must be not empty with ("NA") string.
my database type data is Varchar
Can anyone help me to fix this only with PHP or any other way?
Thanks in advance.
解决方案
In php 5 you can use isset().
$tgl_real = isset($_POST['TGL_REAL'])? $_POST['TGL_REAL']:'NA';
in php 7, you do not need isset(), you can use ??:
$tgl_real = $_POST['TGL_REAL']?? 'NA';
You can also use empty() to check POST data is empty or not.
if(empty($_POST['TGL_REAL'])){
$tgl_real = 'NA';
}else{
$tgl_real = $_POST['TGL_REAL'];
}
In fact, you can set default Constraint in mysql and check to set NOT NULL