This is the queries:
/* ALL THE QUERIES */
$sql_update = "UPDATE tbl_usulan_gb_detail SET
status = ? ,
scan_hasil_skgb = ?,
tgl_selesai = ?,
no_sk_baru = ?,
tgl_sk_baru = ?,
status_koreksi=?
WHERE
id_usulan_gb_detail =?";
$sql_select1 = "SELECT a.*,b.*
FROM tbl_usulan_gb_detail a
LEFT JOIN tbl_usulan_gb b ON b.id_usulan = a.id_usulan
WHERE a.id_usulan_gb_detail = ? ";
$sql_select2 = "SELECT * FROM tbl_pejabat_penandatangan WHERE id = ?";
$sql_insert1 = "INSERT INTO tblriwayatgaji (
id_pegawai,
NIP,
TMTGaji,
NOSKGaji,
TglSKGaji,
PejabatSK,
tgl_sk_usulan,
mkg_tahun,
mkg_bulan
)VALUES(?,?,?,?,?,?,?,?,'?')"; // <----TEST ('') at the last of (?) would throw an error
$sql_select3 = "SELECT * FROM tblriwayatgaji WHERE NIP = ? OR id_pegawai=? AND TMTGaji =? ORDER BY id_riwayat_gaji DESC";
$sql_insert2 = "INSERT INTO
tbl_pegawai_upload_detail(id_pegawai,id_jenis,id_riwayat,scan_sk,catatan_upload)
VALUES(?,?,?,?,?)";
/* END OF QUERIES */
And this is the transaction :
/* NEW WITH EXCEPTION HANDLER */
try {
$mysqli->autocommit(false);
// Update status column of tbl_usulan_gb_detail to 1
$stmt = $mysqli->prepare($sql_update);
$status = 1;
$status_koreksi = 0;
$tgl_selesai = date("Y-m-d");
$stmt->bind_param("issssii", $status, $nama_file, $tgl_selesai, $no_sk, $tgl_sk, $status_koreksi, $id_usulan_gb_detail);
//$stmt->execute(); // execute() is silently error :(
if (!$stmt->execute()) {
something_wrong(htmlspecialchars($mysqli->error));
$mysqli->rollback();
exit();
}
// Get data from tbl_usulan_gb_detail
$stmt = $mysqli->prepare($sql_select1);
$stmt->bind_param("i", $id_usulan_gb_detail);
//$stmt->execute(); // execute() is silently error :(
if (!$stmt->execute()) {
something_wrong(htmlspecialchars($mysqli->error));
exit();
}
$result = $stmt->get_result();
$ds = $result->fetch_assoc();
$nip = $ds["nip"];
$id_pegawai = detail_pegawai_by_nip($ds["nip"],"id_pegawai"); // get the id of pegawai based on inserted nip
$tmt_baru = $ds["tmt_baru"];
$no_sk_usulan = $ds["no_sk_baru"];
$tgl_sk_usulan = $ds["tgl_sk_baru"];//
$mkg_tahun_baru = $ds["tahun_baru"];
$mkg_bulan_baru = $ds["bulan_baru"];
$scan_hasil_skgb = $ds["scan_hasil_skgb"]; //gak ada dipakai dibawah?
// Get jabatan and daerah
$stmt->prepare($sql_select2);
$stmt->bind_param("i", $id=1);
//$stmt->execute();// execute() is silently error :(
if (!$stmt->execute()) {
something_wrong(htmlspecialchars($mysqli->error));
exit();
}
$result = $stmt->get_result();
$peneken = $result->fetch_assoc();
$nama_peneken = $peneken["jabatan"]." ".$peneken["daerah"];//"KEPALA BADAN KEPEGAWAIAN DAERAH KOTA MEDAN";
// Insert data into tblriwayatgaji table
if(!isset($tgl_usulan)){ // set tgl_sk_usulan in tblriwayatgaji (NOT NULL)
$tgl_usulan = "0000-00-00";
}
$stmt = $mysqli->prepare($sql_insert1);
$stmt->bind_param("issssssss", $id_pegawai, $nip, $tmt_baru, $no_sk_usulan, $tgl_sk_usulan, $nama_peneken, $tgl_usulan, $mkg_tahun_baru, $mkg_bulan_baru);
//$stmt->execute(); // execute() is silently error :(
if (!$stmt->execute()) {
something_wrong(htmlspecialchars($mysqli->error));
$mysqli->rollback();
exit();
}
// Get the id_riwayat_gaji from tblriwayatgaji table
$stmt = $mysqli->prepare($sql_select3);
$id = 1;
$stmt->bind_param("sis", $nip, $id_pegawai, $tmt_baru);
if (!$stmt->execute()) {
something_wrong(htmlspecialchars($mysqli->error));
$mysqli->rollback();
exit();
}
$result = $stmt->get_result();
$ds_riwayat = $result->fetch_assoc();
$id_riwayat_gaji = $ds_riwayat["id_riwayat_gaji"];
$id_jenis = 'tblriwayatgaji';
$file_skgb = "sk_gaji_berkala".$id_pegawai."_".$id_riwayat_gaji.".pdf";
// Insert data into tbl_pegawai_upload_detail
$stmt = $mysqli->prepare($sql_insert2);
$catatan_upload = "TEST1234-".date("Y-m-d H:i:s");
$stmt->bind_param("isiss", $id_pegawai, $id_jenis, $id_riwayat_gaji, $file_skgb, $catatan_upload);
//$stmt->execute(); // execute() is silently error :(
if (!$stmt->execute()) {
something_wrong(htmlspecialchars($mysqli->error));
$mysqli->rollback();
exit();
}
$mysqli->commit();
$path_1 = $path . $nama_file;
$path_2 = "../../upload/22.sk_gaji_berkala/".$file_skgb."";
copy($path_1, $path_2);
success_upload("committed");
}catch(Exception $e){
$mysqli->rollback();
something_wrong(throw $e);
}
$stmt->free_result();
$stmt->close();
$mysqli->close();
exit();
/* END EXCEPTION HANDLER */
I deliberately put '?'
at the last value of $sql_insert1
to test would be the $mysqli->rollback();
called if there was an error in the prepare query.
But it didn't run like what i expected. It run $mysqli->commit()
instead.
So i checked my records for all involved tables in process, only "tblriwayatgaji" table that didn't add any data.