I have a PHP script that reads data from an Excel file and inserts it into a MySQL database. The script reads the data correctly and performs the initial insertion into the tb_konsultasi table without issues. However, the subsequent insertion into the tb_detailrusak table based on conditions in the Excel data does not execute as expected.
Below is my complete code:
require_once 'includes/connect.php';
require 'vendor/autoload.php';
use PhpOfficePhpSpreadsheetReaderXlsx;
// Mengaktifkan pelaporan error
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function log_and_echo_error($message) {
error_log($message);
echo $message . "<br>";
}
if (!empty($_FILES['file']['name'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
if (!is_dir($target_dir)) {
mkdir($target_dir, 0777, true);
}
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
$reader = new Xlsx();
try {
$spreadsheet = $reader->load($target_file);
} catch (Exception $e) {
log_and_echo_error("Error loading spreadsheet: " . $e->getMessage());
die("Error loading spreadsheet");
}
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
$koneksi = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);
if ($koneksi->connect_error) {
log_and_echo_error("Connection failed: " . $koneksi->connect_error);
die("Connection failed: " . $koneksi->connect_error);
}
$header = $sheetData[1]; // Simpan header untuk referensi nama atribut
foreach ($sheetData as $index => $row) {
if ($index === 1) continue; // skip header row
if (!empty(array_filter($row))) {
$tanggal_raw = $row['A'];
$nama_perangkat = $row['B'];
$nama_pelanggan = $row['C'];
$kerusakan = $row['AM']; // Column containing kerusakan information
// Konversi format tanggal ke YYYY-MM-DD
$tanggal = date('Y-m-d', strtotime(str_replace('/', '-', $tanggal_raw)));
// Simpan data konsultasi
$stmt_konsultasi = $koneksi->prepare("INSERT INTO tb_konsultasi (tanggal, nama_perangkat, nama_pelanggan, hasil_kerusakan) VALUES (?, ?, ?, ?)");
if (!$stmt_konsultasi) {
log_and_echo_error("Prepare failed for konsultasi: " . $koneksi->error);
continue;
}
$stmt_konsultasi->bind_param("ssss", $tanggal, $nama_perangkat, $nama_pelanggan, $kerusakan);
if (!$stmt_konsultasi->execute()) {
log_and_echo_error("Execute failed for konsultasi: " . $stmt_konsultasi->error);
continue;
}
$id_konsultasi = $stmt_konsultasi->insert_id;
$stmt_konsultasi->close();
// Simpan detail kerusakan berdasarkan gejala
foreach ($row as $key => $value) {
if ($key > 'C' && $key < 'AK' && $value == 'Ya') {
$nama_atribut = $header[$key]; // Ambil nama atribut dari header
$stmt_atribut = $koneksi->prepare("SELECT id_atribut FROM tb_atribut WHERE nama_atribut = ?");
if (!$stmt_atribut) {
log_and_echo_error("Prepare failed for atribut: " . $koneksi->error);
continue;
}
$stmt_atribut->bind_param("s", $nama_atribut);
if (!$stmt_atribut->execute()) {
log_and_echo_error("Execute failed for atribut: " . $stmt_atribut->error);
continue;
}
$result_atribut = $stmt_atribut->get_result();
if ($result_atribut->num_rows > 0) {
$atribut = $result_atribut->fetch_assoc();
$id_atribut = $atribut['id_atribut'];
$stmt_detail = $koneksi->prepare("INSERT INTO tb_detailrusak (id_konsultasi, id_atribut) VALUES (?, ?)");
if (!$stmt_detail) {
log_and_echo_error("Prepare failed for detailrusak: " . $koneksi->error);
continue;
}
$stmt_detail->bind_param("ii", $id_konsultasi, $id_atribut);
if (!$stmt_detail->execute()) {
log_and_echo_error("Execute failed for detailrusak: " . $stmt_detail->error);
continue;
}
$stmt_detail->close();
} else {
log_and_echo_error("Atribut not found: " . $nama_atribut);
}
$stmt_atribut->close();
}
}
}
}
$koneksi->close();
echo json_encode(array(
'status' => 'Success',
'message' => 'Data berhasil diimpor!'
));
} else {
log_and_echo_error("Failed to move uploaded file.");
echo json_encode(array(
'status' => 'Error',
'message' => 'Failed to move uploaded file.'
));
}
} else {
log_and_echo_error("No file selected.");
echo json_encode(array(
'status' => 'Error',
'message' => 'No file selected'
));
}
Issues:
The initial insertion into tb_konsultasi works fine.
The subsequent insertion into tb_detailrusak based on specific conditions does not work as expected.
**What I have tried:
**
Added error logging and echo statements to debug the issue.
Verified that the conditions for inserting into tb_detailrusak are met.
Ensured that the database connection is successful and the initial insertion into tb_konsultasi works.
Checked if tb_atribut contains the relevant data for nama_atribut.
Database Schema:
tb_konsultasi: id_konsultasi (AUTO_INCREMENT, PRIMARY KEY), tanggal, nama_perangkat, nama_pelanggan, hasil_kerusakan
tb_detailrusak: id_detailrusak (AUTO_INCREMENT, PRIMARY KEY), id_konsultasi, id_atribut
tb_atribut: id_atribut (AUTO_INCREMENT, PRIMARY KEY), nama_atribut
Questions
Why isn’t the insertion into tb_detailrusak executing as expected even when the conditions are met?
Are there any improvements or debugging steps I might be missing to resolve this issue?
Any help or insights would be greatly appreciated!