Modul Tugas Pemograman Web Pertemuan 12
Modul Tugas Pemograman Web Pertemuan 12
sekolah.sql
--
-- Database: `sekolah`
--
-- --------------------------------------------------------
--
-- Table structure for table `guru`
--
--
-- Dumping data for table `guru`
--
-- --------------------------------------------------------
--
-- Table structure for table `siswa`
--
--
-- Dumping data for table `siswa`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
--
-- Dumping data for table `users`
--
--
-- Indexes for dumped tables
--
--
-- Indexes for table `guru`
--
ALTER TABLE `guru`
ADD PRIMARY KEY (`nip`);
--
-- Indexes for table `siswa`
--
ALTER TABLE `siswa`
ADD PRIMARY KEY (`nis`),
ADD KEY `nip` (`nip`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `siswa`
--
ALTER TABLE `siswa`
ADD CONSTRAINT `siswa_ibfk_1` FOREIGN KEY (`nip`) REFERENCES `guru` (`nip`);
COMMIT;
<?php
$koneksi = mysqli_connect("localhost", "root", "", "sekolah");
if (!$koneksi) {
die("Koneksi gagal: " . mysqli_connect_error());
}
?>
Buat file index.php
<?php
session_start();
if (!isset($_SESSION['login'])) {
header("Location: login.php");
exit;
}
include 'db.php';
$cari = isset($_GET['cari']) ? $_GET['cari'] : '';
?>
<!DOCTYPE html>
<html>
<head>
<title>Data Siswa & Guru</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
</head>
<body class="container mt-4">
<h3>Data Siswa & Guru Pembimbing</h3>
<a href="tambah_siswa.php" class="btn btn-success mb-2">+ Tambah Siswa</a>
<a href="tambah_guru.php" class="btn btn-secondary mb-2">+ Tambah Guru</a>
<a href="cetak_pdf.php" class="btn btn-danger mb-2" target="_blank">Cetak
PDF</a>
<a href="logout.php" class="btn btn-outline-dark mb-2 float-end">Logout</a>
<form method="GET" class="mb-2">
<input type="text" name="cari" placeholder="Cari nama siswa..." value="<?=
$cari ?>" class="form-control" />
</form>
<table class="table table-bordered table-striped">
<thead class="table-dark">
<tr>
<th>NIS</th>
<th>Nama Siswa</th>
<th>Nama Guru</th>
<th>Alamat Guru</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT siswa.nis, siswa.nama AS nama_siswa, guru.nama AS
nama_guru, guru.alamat
FROM siswa
JOIN guru ON siswa.nip = guru.nip
WHERE siswa.nama LIKE '%$cari%'";
$query = mysqli_query($koneksi, $sql);
while ($row = mysqli_fetch_assoc($query)) {
?>
<tr>
<td><?= $row['nis'] ?></td>
<td><?= $row['nama_siswa'] ?></td>
<td><?= $row['nama_guru'] ?></td>
<td><?= $row['alamat'] ?></td>
<td>
<a href="edit_siswa.php?nis=<?= $row['nis'] ?>" class="btn btn-
warning btn-sm">Edit</a>
<a href="hapus_siswa.php?nis=<?= $row['nis'] ?>" class="btn btn-
danger btn-sm" onclick="return confirm('Hapus data ini?')">Hapus</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
<?php
session_start();
include 'db.php';
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = md5($_POST['password']);
$query = mysqli_query($koneksi, "SELECT * FROM users WHERE
username='$username' AND password='$password'");
if (mysqli_num_rows($query) > 0) {
$_SESSION['login'] = true;
header("Location: index.php");
exit; // Penting: tambahkan exit setelah header redirect
} else {
$error = "Username atau password salah!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body class="bg-light"> <div class="container d-flex justify-content-center
align-items-center min-vh-100">
<div class="row">
<div class="col md-8"> <div class="card p-4 shadow-sm"> <h3
class="text-center mb-4">Login</h3>
<?php if (isset($error)) echo "<div class='alert alert-
danger'>$error</div>"; ?>
<form method="POST">
<div class="mb-3">
<label for="username" class="form-
label">Username</label>
<input type="text" name="username" id="username"
class="form-control" required>
</div>
<div class="mb-3">
<label for="password" class="form-
label">Password</label>
<input type="password" name="password"
id="password" class="form-control" required>
</div>
<div class="d-grid gap-2"> <button class="btn btn-
primary" name="login">Login</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min
.js" integrity="sha384-
YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcSanxUaRkZqS6jC5/JbU"
crossorigin="anonymous"></script>
</body>
</html>
<?php
session_start();
session_destroy();
header("Location: login.php");
?>
<?php
define('FPDF_FONTPATH', 'font/'); // <--- Tambahkan ini
require('fpdf.php');
include 'db.php';
$pdf->SetFont('Arial','B',10);
$pdf->Cell(20,8,'NIS',1);
$pdf->Cell(50,8,'Nama Siswa',1);
$pdf->Cell(50,8,'Nama Guru',1);
$pdf->Cell(60,8,'Alamat Guru',1);
$pdf->Ln();
Jika folder font/ belum ada, kamu bisa unduh dari https://www.fpdf.org
Download terkait fpdf.php dengan file asli dari fpdf.org agar fitur PDF berfungsi sempurna
Pilih untuk download file v1.86 (2023-06-25) ZIP
Kemudian extract file zipnya, akan terlihat isi foldernya sebagai berikut :