Membuat Aplikasi Database Sederhana: Perbedaan antara revisi

Dari Dokumentasi Robie
Loncat ke navigasi Loncat ke pencarian
Tidak ada ringkasan suntingan
Baris 41: Baris 41:
=Buat file index.php=
=Buat file index.php=


<?php
<code><?php</code>
include 'config.php';  
 
<code>include 'config.php';</code>
$query = $pdo->query("SELECT * FROM siswa");
 
//$siswa = $query->fetchAll(PDO::FETCH_ASSOC);
<code>$query = $pdo->query("SELECT * FROM siswa");</code>
 
// Mendapatkan kata kunci dari form pencarian jika ada
<code>//$siswa = $query->fetchAll(PDO::FETCH_ASSOC);</code>
$search = isset($_GET['search']) ? $_GET['search'] : '';
 
<code>// Mendapatkan kata kunci dari form pencarian jika ada</code>
// Query dengan kondisi pencarian jika kata kunci ada
 
if ($search) {
<code>$search = isset($_GET['search']) ? $_GET['search'] : <nowiki>''</nowiki>;</code>
    $query = $pdo->prepare("SELECT * FROM siswa WHERE nama LIKE ? OR kelas LIKE ?");
 
    $query->execute(["%$search%", "%$search%"]);
<code>// Query dengan kondisi pencarian jika kata kunci ada</code>
} else {
 
    $query = $pdo->query("SELECT * FROM siswa");
<code>if ($search) {</code>
}
 
<code>    $query = $pdo->prepare("SELECT * FROM siswa WHERE nama LIKE ? OR kelas LIKE ?");</code>
$siswa = $query->fetchAll(PDO::FETCH_ASSOC);
 
?>
<code>    $query->execute(["%$search%", "%$search%"]);</code>
 
<!DOCTYPE html>
<code>} else {</code>
<html lang="id">
 
<head>
<code>    $query = $pdo->query("SELECT * FROM siswa");</code>
    <meta charset="UTF-8">
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<code>}</code>
    <title>Data Siswa Organisasi</title>
 
   
<code>$siswa = $query->fetchAll(PDO::FETCH_ASSOC);</code>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384- xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
 
</head>
<code>?></code>
<body>
 
<div class="container mt-5">
<code><!DOCTYPE html></code>
    <h3 class="mb-4">Data Siswa Organisasi</h3>
 
  <!-- Notifikasi Pop-Up -->
<code><html lang="id"></code>
    <?php if (isset($_GET['success']) && $_GET['success'] == 1): ?>
 
        <script>
<code><head></code>
            alert("Data berhasil ditambahkan!");
 
        </script>
<code>    <meta charset="UTF-8"></code>
    <?php endif; ?>  
 
 
<code>    <meta name="viewport" content="width=device-width, initial-scale=1.0"></code>
  <!-- Form Pencarian -->
 
    <form method="GET" action="index.php" class="d-flex mb-3">
<code>    <title>Data Siswa Organisasi</title></code>
          <input type="text" name="search" class="form-control me-2" placeholder="Cari nama atau kelas" value="<?= htmlspecialchars($search); ?>">
 
        <button type="submit" class="btn btn-primary">Cari</button>
<code>   </code>
        <a href="index.php" class="btn btn-secondary ms-2">Reset</a>
 
    </form>
<code><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous"></code>
 
      <!-- Tombol Tambah Siswa -->
<code></head></code>
    <a href="tambah.php" class="btn btn-primary mb-3">Tambah Siswa</a>
 
  <table class="table table-bordered"><tr>
<code><body></code>
                <th>ID</th>
 
                <th>Nama</th>
<code><nowiki><div class="container mt-5"></nowiki></code>
                <th>Kelas</th>
 
                <th>No HP</th>
<code>    <nowiki><h2 class="mb-4">Data Siswa Organisasi</h2></nowiki></code>
                <th>Riwayat Organisasi</th>
 
                <th>Jabatan</th>
<code><!-- Notifikasi Pop-Up --></code>
                <th>Tahun Ajaran</th>
 
                <th>Aksi</th>
<code>    <?php if (isset($_GET['success']) && $_GET['success'] == 1): ?></code>
            </tr><tr>
 
                    <td><?= htmlspecialchars($row['id']); ?></td>
<code>        <script></code>
                    <td><?= htmlspecialchars($row['nama']); ?></td>
 
                    <td><?= htmlspecialchars($row['kelas']); ?></td>
<code>            alert("Data berhasil ditambahkan!");</code>
                    <td><?= htmlspecialchars($row['no_hp']); ?></td>
 
                    <td><?= htmlspecialchars($row['riwayat_organisasi']); ?></td>
<code>        </script></code>
                    <td><?= htmlspecialchars($row['jabatan']); ?></td>
 
                    <td><?= htmlspecialchars($row['tahun_ajaran']); ?></td>
<code>    <?php endif; ?></code>
                    <td>
 
                        <a href="edit.php?id=<?= $row['id']; ?>" class="btn btn-warning btn-sm">Edit</a>
<code>  <!-- Form Pencarian --></code>
                        <a href="hapus.php?id=<?= $row['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin menghapus data?');">Hapus</a>
 
                    </td>
<code>    <form method="GET" action="index.php" class="d-flex mb-3"></code>
                </tr></table>
 
</div>
<code>        <input type="text" name="search" class="form-control me-2" placeholder="Cari nama atau kelas" value="<?= htmlspecialchars($search); ?>"></code>
 
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<code>        <button type="submit" class="btn btn-primary">Cari</button></code>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
 
<code>        <a href="index.php" class="btn btn-secondary ms-2">Reset</a></code>
</body>
 
</html>
<code>    </form></code>
 
<code>    <!-- Tombol Tambah Siswa --></code>
 
<code>    <a href="tambah.php" class="btn btn-primary mb-3">Tambah Siswa</a></code>
 
<code><nowiki><table class="table table-bordered"></nowiki></code>
 
<code>        <thead></code>
 
<code>            <nowiki><tr></nowiki></code>
 
<code>                <nowiki><th>ID</th></nowiki></code>
 
<code>                <nowiki><th>Nama</th></nowiki></code>
 
<code>                <nowiki><th>Kelas</th></nowiki></code>
 
<code>                <nowiki><th>No HP</th></nowiki></code>
 
<code>                <nowiki><th>Riwayat Organisasi</th></nowiki></code>
 
<code>                <nowiki><th>Jabatan</th></nowiki></code>
 
<code>                <nowiki><th>Tahun Ajaran</th></nowiki></code>
 
<code>                <nowiki><th>Aksi</th></nowiki></code>
 
<code>            <nowiki></tr></nowiki></code>
 
<code>        </thead></code>
 
<code>        <tbody></code>
 
<code>            <?php foreach ($siswa as $row): ?></code>
 
<code>                <nowiki><tr></nowiki></code>
 
<code>                    <nowiki><td></nowiki><?= htmlspecialchars($row['id']); ?><nowiki></td></nowiki></code>
 
<code>                    <nowiki><td></nowiki><?= htmlspecialchars($row['nama']); ?><nowiki></td></nowiki></code>
 
<code>                    <nowiki><td></nowiki><?= htmlspecialchars($row['kelas']); ?><nowiki></td></nowiki></code>
 
<code>                    <nowiki><td></nowiki><?= htmlspecialchars($row['no_hp']); ?><nowiki></td></nowiki></code>
 
<code>                    <nowiki><td></nowiki><?= htmlspecialchars($row['riwayat_organisasi']); ?><nowiki></td></nowiki></code>
 
<code>                    <nowiki><td></nowiki><?= htmlspecialchars($row['jabatan']); ?><nowiki></td></nowiki></code>
 
<code>                    <nowiki><td></nowiki><?= htmlspecialchars($row['tahun_ajaran']); ?><nowiki></td></nowiki></code>
 
<code>                    <nowiki><td></nowiki></code>
 
<code>                        <a href="edit.php?id=<?= $row['id']; ?>" class="btn btn-warning btn-sm">Edit</a></code>
 
<code>                        <a href="hapus.php?id=<?= $row['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin menghapus data?');">Hapus</a></code>
 
<code>                    <nowiki></td></nowiki></code>
 
<code>                <nowiki></tr></nowiki></code>
 
<code>            <?php endforeach; ?></code>
 
<code>        </tbody></code>
 
<code>    <nowiki></table></nowiki></code>
 
<code><nowiki></div></nowiki></code>
 
<code><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script></code>
 
<code><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script></code>
 
<code></body></code>
 
<code></html></code>




=Buat file edit.php=
=Buat file edit.php=
<?php
 
include 'config.php';  
<code><?php</code>
 
// Ambil ID dari URL
<code>include 'config.php';</code>
$id = $_GET['id'];  
 
<code>// Ambil ID dari URL</code>
// Ambil data siswa dari database
 
$stmt = $pdo->prepare("SELECT * FROM siswa WHERE id = ?");
<code>$id = $_GET['id'];</code>
$stmt->execute([$id]);
 
$siswa = $stmt->fetch();  
<code>// Ambil data siswa dari database</code>
 
// Jika form disubmit, update data siswa
<code>$stmt = $pdo->prepare("SELECT * FROM siswa WHERE id = ?");</code>
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
 
    $nama = $_POST['nama'];
<code>$stmt->execute([$id]);</code>
    $kelas = $_POST['kelas'];
 
    $no_hp = $_POST['no_hp'];
<code>$siswa = $stmt->fetch();</code>
    $riwayat_organisasi = $_POST['riwayat_organisasi'];
 
    $jabatan = $_POST['jabatan'];
<code>// Jika form disubmit, update data siswa</code>
    $tahun_ajaran = $_POST['tahun_ajaran'];
 
<code>if ($_SERVER['REQUEST_METHOD'] == 'POST') {</code>
    // Update data siswa
 
    $stmt = $pdo->prepare("UPDATE siswa SET nama = ?, kelas = ?, no_hp = ?, riwayat_organisasi = ?, jabatan = ?, tahun_ajaran = ? WHERE id = ?");
<code>    $nama = $_POST['nama'];</code>
    $stmt->execute([$nama, $kelas, $no_hp, $riwayat_organisasi, $jabatan, $tahun_ajaran, $id]);
 
<code>    $kelas = $_POST['kelas'];</code>
    // Redirect ke halaman utama setelah update
 
    header("Location: index.php");
<code>    $no_hp = $_POST['no_hp'];</code>
}
 
?>
<code>    $riwayat_organisasi = $_POST['riwayat_organisasi'];</code>
 
<!DOCTYPE html>
<code>    $jabatan = $_POST['jabatan'];</code>
<html lang="id">
 
<head>
<code>    $tahun_ajaran = $_POST['tahun_ajaran'];</code>
    <meta charset="UTF-8">
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<code>    // Update data siswa</code>
    <title>Edit Data Siswa</title>
 
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<code>    $stmt = $pdo->prepare("UPDATE siswa SET nama = ?, kelas = ?, no_hp = ?, riwayat_organisasi = ?, jabatan = ?, tahun_ajaran = ? WHERE id = ?");</code>
</head>
 
<body>
<code>    $stmt->execute([$nama, $kelas, $no_hp, $riwayat_organisasi, $jabatan, $tahun_ajaran, $id]);</code>
<div class="container mt-5">
 
    <h2 class="mb-4">Edit Data Siswa</h2>
<code>    // Redirect ke halaman utama setelah update</code>
    <form method="POST">
 
        <div class="mb-3">
<code>    header("Location: index.php");</code>
            <label>Nama</label>
 
            <input type="text" name="nama" class="form-control" value="<?= htmlspecialchars($siswa['nama']); ?>" required>
<code>}</code>
        </div>
 
        <div class="mb-3">
<code>?></code>
            <label>Kelas</label>
 
            <input type="text" name="kelas" class="form-control" value="<?= htmlspecialchars($siswa['kelas']); ?>" required>
<code><!DOCTYPE html></code>
        </div>
 
        <div class="mb-3">
<code><html lang="id"></code>
            <label>No HP</label>
 
            <input type="text" name="no_hp" class="form-control" value="<?= htmlspecialchars($siswa['no_hp']); ?>" required>
<code><head></code>
        </div>
 
        <div class="mb-3">
<code>    <meta charset="UTF-8"></code>
            <label>Riwayat Organisasi</label>
 
            <nowiki><textarea name="riwayat_organisasi" class="form-control" required><?= htmlspecialchars($siswa['riwayat_organisasi']); ?> </nowiki>
<code>    <meta name="viewport" content="width=device-width, initial-scale=1.0"></code>
</textarea>
 
        </div>
<code>    <title>Edit Data Siswa</title></code>
        <div class="mb-3">
 
            <label>Jabatan</label>
<code>    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous"></code>
            <input type="text" name="jabatan" class="form-control" value="<?= htmlspecialchars($siswa['jabatan']); ?>" required>
 
        </div>
<code></head></code>
        <div class="mb-3">
 
            <label>Tahun Ajaran</label>
<code><body></code>
            <input type="text" name="tahun_ajaran" class="form-control" value="<?= htmlspecialchars($siswa['tahun_ajaran']); ?>" required>
 
        </div>
<code><nowiki><div class="container mt-5"></nowiki></code>
        <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
 
        <a href="index.php" class="btn btn-secondary">Batal</a>
<code>    <nowiki><h2 class="mb-4">Edit Data Siswa</h2></nowiki></code>
    </form>
 
</div>
<code>    <form method="POST"></code>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
 
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
<code>        <nowiki><div class="mb-3"></nowiki></code>
</body>
 
</html>
<code>            <label>Nama</label></code>
 
<code>            <input type="text" name="nama" class="form-control" value="<?= htmlspecialchars($siswa['nama']); ?>" required></code>
 
<code>        <nowiki></div></nowiki></code>
 
<code>        <nowiki><div class="mb-3"></nowiki></code>
 
<code>            <label>Kelas</label></code>
 
<code>            <input type="text" name="kelas" class="form-control" value="<?= htmlspecialchars($siswa['kelas']); ?>" required></code>
 
<code>        <nowiki></div></nowiki></code>
 
<code>        <nowiki><div class="mb-3"></nowiki></code>
 
<code>            <label>No HP</label></code>
 
<code>            <input type="text" name="no_hp" class="form-control" value="<?= htmlspecialchars($siswa['no_hp']); ?>" required></code>
 
<code>        <nowiki></div></nowiki></code>
 
<code>        <nowiki><div class="mb-3"></nowiki></code>
 
<code>            <label>Riwayat Organisasi</label></code>
 
<code>            <nowiki><textarea name="riwayat_organisasi" class="form-control" required><?=htmlspecialchars($siswa['riwayat_organisasi']); ?></textarea></nowiki></code>
 
<code>        <nowiki></div></nowiki></code>
 
<code>        <nowiki><div class="mb-3"></nowiki></code>
 
<code>            <label>Jabatan</label></code>
 
<code>            <input type="text" name="jabatan" class="form-control" value="<?= htmlspecialchars($siswa['jabatan']); ?>" required></code>
 
<code>        <nowiki></div></nowiki></code>
 
<code>        <nowiki><div class="mb-3"></nowiki></code>
 
<code>            <label>Tahun Ajaran</label></code>
 
<code>            <input type="text" name="tahun_ajaran" class="form-control" value="<?= htmlspecialchars($siswa['tahun_ajaran']); ?>" required></code>
 
<code>        <nowiki></div></nowiki></code>
 
<code>        <button type="submit" class="btn btn-primary">Simpan Perubahan</button></code>
 
<code>        <a href="index.php" class="btn btn-secondary">Batal</a></code>
 
<code>    </form></code>
 
<code><nowiki></div></nowiki></code>
 
<code><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script></code>
 
<code><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script></code>
 
<code></body></code>
 
<code></html></code>





Revisi per 7 November 2024 07.07

Sebelum memulai pastikan apache2, mariadb, dan php sudah terinstall dengan baik


Siapkan Database

masuk ke mariadb lalu ketikkan perintah berikut

CREATE DATABASE organisasi_siswa;
CREATE USER 'siswa'@'localhost' IDENTIFIED BY '12345';
GRANT ALL PRIVILEGES ON organisasi_siswa.* TO 'siswa'@'localhost';
USE organisasi_siswa;
CREATE TABLE siswa (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nama VARCHAR(100) NOT NULL,
    kelas VARCHAR(15) NOT NULL,
    no_hp VARCHAR(15) NOT NULL,
    riwayat_organisasi TEXT NOT NULL,
    jabatan VARCHAR(50) NOT NULL,
    tahun_ajaran VARCHAR(9) NOT NULL
);

Buaf file config.php

isi dengan kode berikut

<?php
// config.php
$host = 'localhost';
$dbname = 'organisasi_siswa';
$username = 'siswa'; // sesuaikan dengan username MariaDB Anda
$password = '12345';     // sesuaikan dengan password MariaDB Anda

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Koneksi gagal: " . $e->getMessage());
}
?>


Buat file index.php

<?php

include 'config.php';

$query = $pdo->query("SELECT * FROM siswa");

//$siswa = $query->fetchAll(PDO::FETCH_ASSOC);

// Mendapatkan kata kunci dari form pencarian jika ada

$search = isset($_GET['search']) ? $_GET['search'] : '';

// Query dengan kondisi pencarian jika kata kunci ada

if ($search) {

    $query = $pdo->prepare("SELECT * FROM siswa WHERE nama LIKE ? OR kelas LIKE ?");

    $query->execute(["%$search%", "%$search%"]);

} else {

    $query = $pdo->query("SELECT * FROM siswa");

}

$siswa = $query->fetchAll(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>

<html lang="id">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Data Siswa Organisasi</title>

   

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

</head>

<body>

<div class="container mt-5">

    <h2 class="mb-4">Data Siswa Organisasi</h2>

    <?php if (isset($_GET['success']) && $_GET['success'] == 1): ?>

        <script>

            alert("Data berhasil ditambahkan!");

        </script>

    <?php endif; ?>

 

    <form method="GET" action="index.php" class="d-flex mb-3">

        <input type="text" name="search" class="form-control me-2" placeholder="Cari nama atau kelas" value="<?= htmlspecialchars($search); ?>">

        <button type="submit" class="btn btn-primary">Cari</button>

        <a href="index.php" class="btn btn-secondary ms-2">Reset</a>

    </form>

   

    <a href="tambah.php" class="btn btn-primary mb-3">Tambah Siswa</a>

<table class="table table-bordered">

        <thead>

            <tr>

                <th>ID</th>

                <th>Nama</th>

                <th>Kelas</th>

                <th>No HP</th>

                <th>Riwayat Organisasi</th>

                <th>Jabatan</th>

                <th>Tahun Ajaran</th>

                <th>Aksi</th>

            </tr>

        </thead>

        <tbody>

            <?php foreach ($siswa as $row): ?>

                <tr>

                    <td><?= htmlspecialchars($row['id']); ?></td>

                    <td><?= htmlspecialchars($row['nama']); ?></td>

                    <td><?= htmlspecialchars($row['kelas']); ?></td>

                    <td><?= htmlspecialchars($row['no_hp']); ?></td>

                    <td><?= htmlspecialchars($row['riwayat_organisasi']); ?></td>

                    <td><?= htmlspecialchars($row['jabatan']); ?></td>

                    <td><?= htmlspecialchars($row['tahun_ajaran']); ?></td>

                    <td>

                        <a href="edit.php?id=<?= $row['id']; ?>" class="btn btn-warning btn-sm">Edit</a>

                        <a href="hapus.php?id=<?= $row['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin menghapus data?');">Hapus</a>

                    </td>

                </tr>

            <?php endforeach; ?>

        </tbody>

    </table>

</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>

</body>

</html>


Buat file edit.php

<?php

include 'config.php';

// Ambil ID dari URL

$id = $_GET['id'];

// Ambil data siswa dari database

$stmt = $pdo->prepare("SELECT * FROM siswa WHERE id = ?");

$stmt->execute([$id]);

$siswa = $stmt->fetch();

// Jika form disubmit, update data siswa

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $nama = $_POST['nama'];

    $kelas = $_POST['kelas'];

    $no_hp = $_POST['no_hp'];

    $riwayat_organisasi = $_POST['riwayat_organisasi'];

    $jabatan = $_POST['jabatan'];

    $tahun_ajaran = $_POST['tahun_ajaran'];

    // Update data siswa

    $stmt = $pdo->prepare("UPDATE siswa SET nama = ?, kelas = ?, no_hp = ?, riwayat_organisasi = ?, jabatan = ?, tahun_ajaran = ? WHERE id = ?");

    $stmt->execute([$nama, $kelas, $no_hp, $riwayat_organisasi, $jabatan, $tahun_ajaran, $id]);

    // Redirect ke halaman utama setelah update

    header("Location: index.php");

}

?>

<!DOCTYPE html>

<html lang="id">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Edit Data Siswa</title>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

</head>

<body>

<div class="container mt-5">

    <h2 class="mb-4">Edit Data Siswa</h2>

    <form method="POST">

        <div class="mb-3">

            <label>Nama</label>

            <input type="text" name="nama" class="form-control" value="<?= htmlspecialchars($siswa['nama']); ?>" required>

        </div>

        <div class="mb-3">

            <label>Kelas</label>

            <input type="text" name="kelas" class="form-control" value="<?= htmlspecialchars($siswa['kelas']); ?>" required>

        </div>

        <div class="mb-3">

            <label>No HP</label>

            <input type="text" name="no_hp" class="form-control" value="<?= htmlspecialchars($siswa['no_hp']); ?>" required>

        </div>

        <div class="mb-3">

            <label>Riwayat Organisasi</label>

            <textarea name="riwayat_organisasi" class="form-control" required><?=htmlspecialchars($siswa['riwayat_organisasi']); ?></textarea>

        </div>

        <div class="mb-3">

            <label>Jabatan</label>

            <input type="text" name="jabatan" class="form-control" value="<?= htmlspecialchars($siswa['jabatan']); ?>" required>

        </div>

        <div class="mb-3">

            <label>Tahun Ajaran</label>

            <input type="text" name="tahun_ajaran" class="form-control" value="<?= htmlspecialchars($siswa['tahun_ajaran']); ?>" required>

        </div>

        <button type="submit" class="btn btn-primary">Simpan Perubahan</button>

        <a href="index.php" class="btn btn-secondary">Batal</a>

    </form>

</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>

</body>

</html>


Buat file hapus.php

<?php
include 'config.php';

$id = $_GET['id'];
$stmt = $pdo->prepare("DELETE FROM siswa WHERE id = ?");
$stmt->execute([$id]);

header("Location: index.php");
?>