Website CRUD Sederhana Data Mahasiswa
Struktur Folder
Simpan folder dengan nama crud_mahasiswa di dalam htdocs XAMPP.
crud_mahasiswa/
│
├── koneksi.php
├── index.php
├── tambah.php
├── simpan.php
├── edit.php
├── update.php
├── hapus.php
├── style.css
└── database.sql
1. Database MySQL
Buat file database.sql lalu import ke phpMyAdmin.
CREATE DATABASE crud_mahasiswa;
USE crud_mahasiswa;
CREATE TABLE mahasiswa (
id INT AUTO_INCREMENT PRIMARY KEY,
nama VARCHAR(100) NOT NULL,
nim VARCHAR(20) NOT NULL,
jurusan VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL
);
INSERT INTO mahasiswa (nama, nim, jurusan, email) VALUES
('Budi Santoso', '2023001', 'Teknik Informatika', 'budi@example.com'),
('Siti Aminah', '2023002', 'Sistem Informasi', 'siti@example.com');
2. Koneksi Database
koneksi.php
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'crud_mahasiswa';
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die('Koneksi gagal: ' . $conn->connect_error);
}
?>
3. Halaman Utama (Read)
index.php
<?php include 'koneksi.php'; ?>
<!DOCTYPE html>
<html>
<head>
<title>CRUD Mahasiswa</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h2>Data Mahasiswa</h2>
<a href="tambah.php" class="btn tambah">+ Tambah Mahasiswa</a>
<table>
<tr>
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jurusan</th>
<th>Email</th>
<th>Aksi</th>
</tr>
<?php
$no = 1;
$result = $conn->query('SELECT * FROM mahasiswa');
while($row = $result->fetch_assoc()):
?>
<tr>
<td><?= $no++; ?></td>
<td><?= htmlspecialchars($row['nama']); ?></td>
<td><?= htmlspecialchars($row['nim']); ?></td>
<td><?= htmlspecialchars($row['jurusan']); ?></td>
<td><?= htmlspecialchars($row['email']); ?></td>
<td>
<a href="edit.php?id=<?= $row['id']; ?>" class="btn edit">Edit</a>
<a href="hapus.php?id=<?= $row['id']; ?>" class="btn hapus"
onclick="return confirm('Yakin ingin menghapus data ini?')">Hapus</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
</body>
</html>
4. Form Tambah Data
tambah.php
<!DOCTYPE html>
<html>
<head>
<title>Tambah Mahasiswa</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h2>Tambah Mahasiswa</h2>
<form action="simpan.php" method="POST">
<label>Nama</label>
<input type="text" name="nama" required>
<label>NIM</label>
<input type="text" name="nim" required>
<label>Jurusan</label>
<input type="text" name="jurusan" required>
<label>Email</label>
<input type="email" name="email" required>
<button type="submit" class="btn tambah">Simpan</button>
<a href="index.php" class="btn">Kembali</a>
</form>
</div>
</body>
</html>
5. Proses Simpan (Create)
simpan.php
<?php
include 'koneksi.php';
$nama = $_POST['nama'];
$nim = $_POST['nim'];
$jurusan = $_POST['jurusan'];
$email = $_POST['email'];
$stmt = $conn->prepare(
'INSERT INTO mahasiswa (nama, nim, jurusan, email) VALUES (?, ?, ?, ?)'
);
$stmt->bind_param('ssss', $nama, $nim, $jurusan, $email);
if ($stmt->execute()) {
header('Location: index.php');
} else {
echo 'Gagal menyimpan data';
}
?>
6. Form Edit Data
edit.php
<?php
include 'koneksi.php';
$id = $_GET['id'];
$stmt = $conn->prepare('SELECT * FROM mahasiswa WHERE id=?');
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Mahasiswa</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h2>Edit Mahasiswa</h2>
<form action="update.php" method="POST">
<input type="hidden" name="id" value="<?= $data['id']; ?>">
<label>Nama</label>
<input type="text" name="nama" value="<?= htmlspecialchars($data['nama']); ?>" required>
<label>NIM</label>
<input type="text" name="nim" value="<?= htmlspecialchars($data['nim']); ?>" required>
<label>Jurusan</label>
<input type="text" name="jurusan" value="<?= htmlspecialchars($data['jurusan']); ?>" required>
<label>Email</label>
<input type="email" name="email" value="<?= htmlspecialchars($data['email']); ?>" required>
<button type="submit" class="btn edit">Update</button>
<a href="index.php" class="btn">Kembali</a>
</form>
</div>
</body>
</html>
7. Proses Update
update.php
<?php
include 'koneksi.php';
$id = $_POST['id'];
$nama = $_POST['nama'];
$nim = $_POST['nim'];
$jurusan = $_POST['jurusan'];
$email = $_POST['email'];
$stmt = $conn->prepare(
'UPDATE mahasiswa SET nama=?, nim=?, jurusan=?, email=? WHERE id=?'
);
$stmt->bind_param('ssssi', $nama, $nim, $jurusan, $email, $id);
if ($stmt->execute()) {
header('Location: index.php');
} else {
echo 'Gagal mengupdate data';
}
?>
8. Hapus Data
hapus.php
<?php
include 'koneksi.php';
$id = $_GET['id'];
$stmt = $conn->prepare('DELETE FROM mahasiswa WHERE id=?');
$stmt->bind_param('i', $id);
if ($stmt->execute()) {
header('Location: index.php');
} else {
echo 'Gagal menghapus data';
}
?>
9. Desain CSS
style.css
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
}
.container {
width: 80%;
margin: 30px auto;
background: #fff;
padding: 20px;
border-radius: 8px;
}
h2 {
text-align: center;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ddd;
}
th {
background: #007bff;
color: white;
padding: 10px;
}
td {
padding: 10px;
text-align: center;
}
input {
width: 100%;
padding: 10px;
margin: 8px 0 15px;
box-sizing: border-box;
}
.btn {
padding: 8px 12px;
text-decoration: none;
border-radius: 4px;
color: white;
display: inline-block;
}
.tambah {
background: #28a745;
}
.edit {
background: #ffc107;
color: black;
}
.hapus {
background: #dc3545;
}
Cara Menjalankan di XAMPP
Jalankan Apache dan MySQL pada XAMPP.
Buka phpMyAdmin:
http://localhost/phpmyadminImport file
database.sql.Simpan folder crud_mahasiswa ke:
C:\xampp\htdocs\Buka browser dan akses:
http://localhost/crud_mahasiswa
Fitur yang Sudah Ada
✅ Tambah data mahasiswa
✅ Tampilkan data mahasiswa
✅ Edit data mahasiswa
✅ Hapus data mahasiswa
✅ Validasi field wajib isi (
required)✅ Konfirmasi hapus
✅ Prepared statement untuk keamanan dasar
✅ Tampilan sederhana dan responsif dasar
Website CRUD ini sudah lengkap dan dapat langsung digunakan sebagai tugas kuliah atau latihan dasar pemrograman web PHP & MySQL.