Membuat CRUD dengan PHP dan MySQL Lengkap dengan Fitur Upload Gambar, Validasi, dan Ekspor File

Table of Contents

Fitur ekspor ini penting karena sering kali data perlu didownload untuk keperluan laporan. Format umum ekspor adalah:

  1. CSV → sederhana, bisa dibuka di Excel.

  2. Excel (.xlsx) → lebih rapi, cocok untuk laporan.

  3. PDF → format resmi, tidak mudah diubah.

Mari kita lengkapi aplikasi CRUD dengan fitur ekspor data supaya lebih profesional. Dengan ekspor, admin bisa mengunduh data dari database ke format CSV, Excel (.xlsx), maupun PDF untuk laporan.

1. Tambahkan Tombol Ekspor di index.php

Di halaman daftar mahasiswa (index.php), tambahkan tombol untuk ekspor:

<a href="tambah.php">Tambah Data</a> | 
<a href="export_csv.php">Ekspor ke CSV</a> | 
<a href="export_excel.php">Ekspor ke Excel</a> | 
<a href="export_pdf.php">Ekspor ke PDF</a>

2. Ekspor ke CSV

Buat file baru export_csv.php

<?php
include 'koneksi.php';

// Header agar otomatis download CSV
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=data_mahasiswa.csv");
header("Pragma: no-cache");
header("Expires: 0");

$output = fopen("php://output", "w");

// Header kolom
fputcsv($output, ['ID', 'Nama', 'NIM', 'Jurusan', 'Foto']);

// Ambil data dari database
$query = "SELECT * FROM mahasiswa";
$result = mysqli_query($koneksi, $query);

while ($row = mysqli_fetch_assoc($result)) {
    fputcsv($output, $row);
}

fclose($output);
exit;
?>

✅ Jika diakses, akan langsung mendownload data_mahasiswa.csv.

3. Ekspor ke Excel (.xlsx)

Untuk ekspor ke Excel, kita gunakan PhpSpreadsheet.

Instalasi via Composer:

composer require phpoffice/phpspreadsheet

Buat file export_excel.php

<?php
include 'koneksi.php';
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Header kolom
$sheet->setCellValue('A1', 'ID');
$sheet->setCellValue('B1', 'Nama');
$sheet->setCellValue('C1', 'NIM');
$sheet->setCellValue('D1', 'Jurusan');
$sheet->setCellValue('E1', 'Foto');

$query = "SELECT * FROM mahasiswa";
$result = mysqli_query($koneksi, $query);

$rowIndex = 2;
while ($row = mysqli_fetch_assoc($result)) {
    $sheet->setCellValue('A'.$rowIndex, $row['id']);
    $sheet->setCellValue('B'.$rowIndex, $row['nama']);
    $sheet->setCellValue('C'.$rowIndex, $row['nim']);
    $sheet->setCellValue('D'.$rowIndex, $row['jurusan']);
    $sheet->setCellValue('E'.$rowIndex, $row['foto']);
    $rowIndex++;
}

// Download file
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="data_mahasiswa.xlsx"');
$writer->save("php://output");
exit;
?>

✅ Hasilnya berupa data_mahasiswa.xlsx yang bisa langsung dibuka di Microsoft Excel.

4. Ekspor ke PDF

Gunakan library FPDF.

Download FPDF

Unduh dari http://www.fpdf.org/ dan letakkan di folder /fpdf.

Buat file export_pdf.php

<?php
include 'koneksi.php';
require('fpdf/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();

// Judul
$pdf->SetFont('Arial', 'B', 14);
$pdf->Cell(190, 10, 'Data Mahasiswa', 0, 1, 'C');
$pdf->Ln(5);

// Header tabel
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(10, 10, 'ID', 1);
$pdf->Cell(50, 10, 'Nama', 1);
$pdf->Cell(30, 10, 'NIM', 1);
$pdf->Cell(40, 10, 'Jurusan', 1);
$pdf->Cell(60, 10, 'Foto', 1);
$pdf->Ln();

// Isi tabel
$pdf->SetFont('Arial', '', 10);
$query = "SELECT * FROM mahasiswa";
$result = mysqli_query($koneksi, $query);

while ($row = mysqli_fetch_assoc($result)) {
    $pdf->Cell(10, 10, $row['id'], 1);
    $pdf->Cell(50, 10, $row['nama'], 1);
    $pdf->Cell(30, 10, $row['nim'], 1);
    $pdf->Cell(40, 10, $row['jurusan'], 1);
    $pdf->Cell(60, 10, $row['foto'], 1);
    $pdf->Ln();
}

$pdf->Output();
?>

✅ Jika diakses, akan mendownload data_mahasiswa.pdf.

Kesimpulan

Sekarang aplikasi CRUD kita sudah super lengkap:

  • CRUD dasar (Create, Read, Update, Delete).

  • Upload gambar dengan validasi input + validasi file (tipe & ukuran).

  • Ekspor data ke CSV, Excel, dan PDF.

Dengan fitur ekspor ini, aplikasi CRUD sudah bisa dipakai untuk kebutuhan laporan resmi maupun pengolahan data di Excel.

Posting Komentar