Update fixdata.php
This commit is contained in:
+89
-17
@@ -3,29 +3,101 @@ require_once 'koneksi.php';
|
||||
|
||||
echo "<h2>Fix Kolom bantuan_tersalur</h2>";
|
||||
|
||||
$columns = [
|
||||
"status_konfirmasi" => "ALTER TABLE bantuan_tersalur ADD COLUMN status_konfirmasi ENUM('Menunggu','Dikonfirmasi','Ditolak') NOT NULL DEFAULT 'Menunggu' AFTER status",
|
||||
"nama_penerima_konfirmasi" => "ALTER TABLE bantuan_tersalur ADD COLUMN nama_penerima_konfirmasi VARCHAR(150) DEFAULT NULL AFTER status_konfirmasi",
|
||||
"catatan_konfirmasi" => "ALTER TABLE bantuan_tersalur ADD COLUMN catatan_konfirmasi TEXT DEFAULT NULL AFTER nama_penerima_konfirmasi",
|
||||
"dikonfirmasi_oleh" => "ALTER TABLE bantuan_tersalur ADD COLUMN dikonfirmasi_oleh INT DEFAULT NULL AFTER catatan_konfirmasi",
|
||||
"tgl_konfirmasi" => "ALTER TABLE bantuan_tersalur ADD COLUMN tgl_konfirmasi TIMESTAMP NULL DEFAULT NULL AFTER dikonfirmasi_oleh"
|
||||
];
|
||||
function columnExists($conn, $table, $column) {
|
||||
$table = $conn->real_escape_string($table);
|
||||
$column = $conn->real_escape_string($column);
|
||||
|
||||
foreach ($columns as $column => $sql) {
|
||||
$cek = $conn->query("SHOW COLUMNS FROM bantuan_tersalur LIKE '$column'");
|
||||
$result = $conn->query("SHOW COLUMNS FROM `$table` LIKE '$column'");
|
||||
return $result && $result->num_rows > 0;
|
||||
}
|
||||
|
||||
if ($cek && $cek->num_rows > 0) {
|
||||
function tableExists($conn, $table) {
|
||||
$table = $conn->real_escape_string($table);
|
||||
|
||||
$result = $conn->query("SHOW TABLES LIKE '$table'");
|
||||
return $result && $result->num_rows > 0;
|
||||
}
|
||||
|
||||
function addColumnIfMissing($conn, $table, $column, $definition, $afterColumn = null) {
|
||||
if (columnExists($conn, $table, $column)) {
|
||||
echo "<p style='color:gray'>Kolom <b>$column</b> sudah ada, dilewati.</p>";
|
||||
return;
|
||||
}
|
||||
|
||||
$afterSql = "";
|
||||
|
||||
if ($afterColumn && columnExists($conn, $table, $afterColumn)) {
|
||||
$afterSql = " AFTER `$afterColumn`";
|
||||
}
|
||||
|
||||
$sql = "ALTER TABLE `$table` ADD COLUMN `$column` $definition $afterSql";
|
||||
|
||||
if ($conn->query($sql)) {
|
||||
echo "<p style='color:green'>Kolom <b>$column</b> berhasil ditambahkan.</p>";
|
||||
} else {
|
||||
if ($conn->query($sql)) {
|
||||
echo "<p style='color:green'>Kolom <b>$column</b> berhasil ditambahkan.</p>";
|
||||
} else {
|
||||
echo "<p style='color:red'>Gagal tambah kolom <b>$column</b>: " . htmlspecialchars($conn->error) . "</p>";
|
||||
}
|
||||
echo "<p style='color:red'>Gagal tambah kolom <b>$column</b>: " . htmlspecialchars($conn->error) . "</p>";
|
||||
}
|
||||
}
|
||||
|
||||
$table = "bantuan_tersalur";
|
||||
|
||||
if (!tableExists($conn, $table)) {
|
||||
die("<p style='color:red'>Tabel <b>bantuan_tersalur</b> belum ada. Import database.sql dulu.</p>");
|
||||
}
|
||||
|
||||
// Tambahkan kolom status dulu karena status_konfirmasi ingin diletakkan setelah status.
|
||||
addColumnIfMissing(
|
||||
$conn,
|
||||
$table,
|
||||
"status",
|
||||
"ENUM('Pending','Tersalurkan') NOT NULL DEFAULT 'Tersalurkan'",
|
||||
"created_at"
|
||||
);
|
||||
|
||||
addColumnIfMissing(
|
||||
$conn,
|
||||
$table,
|
||||
"status_konfirmasi",
|
||||
"ENUM('Menunggu','Dikonfirmasi','Ditolak') NOT NULL DEFAULT 'Menunggu'",
|
||||
"status"
|
||||
);
|
||||
|
||||
addColumnIfMissing(
|
||||
$conn,
|
||||
$table,
|
||||
"nama_penerima_konfirmasi",
|
||||
"VARCHAR(150) DEFAULT NULL",
|
||||
"status_konfirmasi"
|
||||
);
|
||||
|
||||
addColumnIfMissing(
|
||||
$conn,
|
||||
$table,
|
||||
"catatan_konfirmasi",
|
||||
"TEXT DEFAULT NULL",
|
||||
"nama_penerima_konfirmasi"
|
||||
);
|
||||
|
||||
addColumnIfMissing(
|
||||
$conn,
|
||||
$table,
|
||||
"dikonfirmasi_oleh",
|
||||
"INT DEFAULT NULL",
|
||||
"catatan_konfirmasi"
|
||||
);
|
||||
|
||||
addColumnIfMissing(
|
||||
$conn,
|
||||
$table,
|
||||
"tgl_konfirmasi",
|
||||
"TIMESTAMP NULL DEFAULT NULL",
|
||||
"dikonfirmasi_oleh"
|
||||
);
|
||||
|
||||
$conn->query("UPDATE `$table` SET status = 'Tersalurkan' WHERE status IS NULL");
|
||||
$conn->query("UPDATE `$table` SET status_konfirmasi = 'Menunggu' WHERE status_konfirmasi IS NULL");
|
||||
|
||||
echo "<hr>";
|
||||
echo "<p><b>Selesai.</b> Coba buka dashboard relawan lagi.</p>";
|
||||
echo "<p>Setelah berhasil, hapus file <code>fix_kolom_bantuan.php</code> dari repo.</p>";
|
||||
echo "<p style='color:green;font-weight:bold'>Selesai. Coba buka dashboard relawan lagi.</p>";
|
||||
echo "<p>Setelah berhasil, hapus file <code>fixdata.php</code> dari repo lalu redeploy.</p>";
|
||||
?>
|
||||
Reference in New Issue
Block a user