Minggu, 14 Februari 2021

Edit Data pada PHP

   Assalamu'alaikum, hallo semuanya! Kembali lagi dengan saya Fadli Mahesa, seorang siswa yang bersekolah di SMKN 4 Bandung dan duduk di bangku kelas XI RPL 3. Kali ini saya akan membagikan lagi sedikit ilmu di dunia percodingan, yaitu tentang Contoh Edit Data pada PHP. Mari kita bahas!


Syntax library.php :


<?php
session_start();

$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'db_pwpb21';

$mysqli = mysqli_connect($host, $user, $pass, $db) or die('Tidak dapat Koneksi ke database');
?>

Syntax v_index.php :


<!DOCTYPE html>
<html>
<head>
    <title>PWPB Praktikum 16, By Fadli Mahesa</title>
    <link rel="stylesheet" type="text/css" href="assets/ale.css">
</head>
<body>
    <h1 class="centertext">Database Siswa</h1>
    <h2>Data yang ada</h2>
    <table border="1">
        <thread>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Nis</th>
                <th scope="col">Nama Lengkap</th>
                <th scope="col">Gender</th>
                <th scope="col">Kelas</th>
                <th scope="col">Jurusan</th>
                <th scope="col">Alamat</th>
                <th scope="col">Gol Darah</th>
                <th scope="col">Nama Orang Tua</th>
            </tr>
        </thread>
        <tbody>
            <?php
                $i = 1;
                while ($siswa = $listSiswa->fetch_array()) {
            ?>
            <tr>
                <th scope="row"><?= $i++; ?></th>
                <td><?= $siswa['nis']; ?></td>
                <td><?= $siswa['nama_lengkap']; ?></td>
                <td><?= $siswa['jenis_kelamin']; ?></td>
                <td><?= $siswa['kelas']; ?></td>
                <td><?= $siswa['jurusan']; ?></td>
                <td><?= $siswa['alamat']; ?></td>
                <td><?= $siswa['gol_darah']; ?></td>
                <td><?= $siswa['nama_ortu']; ?></td>
                <td>
                    <a href="edit.php?nis=<?= $siswa["nis"]; ?>" class="badge badge-primary">Edit</a>
                    <a href="delete.php?nis=<?= $siswa["nis"]; ?>" class="badge badge-danger" onclick="return confirm('Apakah anda yakin ingin menghapus data tersebut ?')">Delete</a></td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
    <br>
    <h2>Aksi yang bisa dilakukan</h2>
    <a href="tambah.php">Tambah Data</a>
</body>
</html>

Syntax v_tambah.php :


<!DOCTYPE html>
<html>
<head>
    <title>Praktikum 16, By Fadli Mahesa</title>
    <link rel="stylesheet" type="text/css" href="assets/ale.css">
</head>
<body>
    <?php
        $action = '$tambah.php';
        if (!empty($siswa)) $action = 'edit.php'
    ?>
    <form method="POST" action="<?= $action ?>">
        <div>
            <div >
                <span>Nis</span>
            </div>
            <input type="text" placeholder="Masukkan Nomor induk siswa" name="nis" value="<?= @$siswa['nis'] ?>" required>
        </div>
        <div>
            <div>
                <span>Nama Lengkap</span>
            </div>
            <input type="text" placeholder="Masukkan Nama Lengkap" name="nama_lengkap" value="<?= @$siswa['nama_lengkap'] ?>" required>
        </div>
        <div>
            <label>
            <h5>Jenis Kelamin :</h5>
            <input type="radio" name="jenis_kelamin" value="L" <?= @$siswa['jenis_kelamin'] == 'L' ? 'checked' : '' ?> required> Laki-laki
            </label>
            <label>
                <input type="radio" name="jenis_kelamin" value="P" <?= @$siswa['jenis_kelamin'] == 'P' ? 'checked' : '' ?>> Perempuan
            </label>
        </div>
        <div>
            <div>
                <label>Kelas</label>
            </div>
            <select class="custom-select" id="inputGroupSelect01" name="kelas" required>
                <option value="XI MM" <?= @$siswa['kelas'] == 'XI MM' ? 'selected' : '' ?>>XI MM</option>
                <option value="XI RPL 1" <?= @$siswa['kelas'] == 'XI RPL 1' ? 'selected' : '' ?>>XI RPL 1</option>
                <option value="XI RPL 2" <?= @$siswa['kelas'] == 'XI RPL 2' ? 'selected' : '' ?>>XI RPL 2</option>
                <option value="XI RPL 3" <?= @$siswa['kelas'] == 'XI RPL 3' ? 'selected' : '' ?>>XI RPL 3</option>
                <option value="XI TKJ 1" <?= @$siswa['kelas'] == 'XI TKJ 1' ? 'selected' : '' ?>>XI TKJ 1</option>
                <option value="XI TKJ 2" <?= @$siswa['kelas'] == 'XI TKJ 2' ? 'selected' : '' ?>>XI TKJ 2</option>
                <option value="XI TOI 1" <?= @$siswa['kelas'] == 'XI TOI 1' ? 'selected' : '' ?>>XI TOI 1</option>
                <option value="XI TOI 2" <?= @$siswa['kelas'] == 'XI TOI 2' ? 'selected' : '' ?>>XI TOI 2</option>
                <option value="XI TITL 1" <?= @$siswa['kelas'] == 'XI TITL 1' ? 'selected' : '' ?>>XI TITL 1</option>
                <option value="XI TITL 2" <?= @$siswa['kelas'] == 'XI TITL 2' ? 'selected' : '' ?>>XI TITL 2</option>
                <option value="XI AVI 1" <?= @$siswa['kelas'] == 'XI AVI 1' ? 'selected' : '' ?>>XI AVI 1</option>
                <option value="XI AVI 2" <?= @$siswa['kelas'] == 'XI AVI 2' ? 'selected' : '' ?>>XI AVI 2</option>
                <option value="XI AVI 3" <?= @$siswa['kelas'] == 'XI AVI 3' ? 'selected' : '' ?>>XI AVI 3</option>
                <option value="XI AVI 4" <?= @$siswa['kelas'] == 'XI AVI 4' ? 'selected' : '' ?>>XI AVI 4</option>
            </select>
        </div>
        <div>
            <div>
                <span>Jurusan</span>
            </div>
                <input type="text" placeholder="Masukkan Jurusan anda" name="jurusan" value="<?= @$siswa['jurusan']; ?>" required>
        </div>
        <div>
            <div>
                <span>Alamat</span>
            </div>
            <input type="text" placeholder="Masukkan Alamat Rumah" name="alamat" value="<?= @$siswa['alamat'] ?>" required>
        </div>
        <div>
            <div>
                <label>Golongan Darah</label>
            </div>
            <select name="gol_darah" required>
                <option value="A" <?= @$siswa['gol_darah'] == 'A' ? 'selected' : '' ?>>A</option>
                <option value="B" <?= @$siswa['gol_darah'] == 'B' ? 'selected' : '' ?>>B</option>
                <option value="AB" <?= @$siswa['gol_darah'] == 'AB' ? 'selected' : '' ?>>AB</option>
                <option value="O" <?= @$siswa['gol_darah'] == 'O' ? 'selected' : '' ?>>O</option>
            </select>
        </div>
        <div>
            <div>
                <span>Nama Orang Tua</span>
            </div>
                <input type="text" placeholder="Masukkan Nama Orang Tua Anda" name="nama_ortu" value="<?= @$siswa['nama_ortu']; ?>" required>
        </div>
        <button type="submit" value="Simpan">Simpan</button>
    </form>
    <a href="index.php">Kembali ke data</a>
    <script>
        const del = document.querySelector(".del");
        const overwrite = document.createElement("input");
        const inp = document.querySelector(".inp");
        if (del.value != '') {
            del.setAttribute("type", "hidden");
            overwrite.setAttribute("type", "text");
            overwrite.setAttribute("class", "form-control");
            overwrite.setAttribute("value", "<?= $siswa["nis"]; ?>");
            overwrite.setAttribute("disabled", "");
            inp.appendChild(overwrite);
        }
    </script>
</body>
</html>

Syntax index.php :


<?php
include 'lib/library.php';

$sql = 'SELECT * FROM siswa';
$listSiswa = $mysqli ->query($sql);

include 'views/v_index.php';
?>

Syntax tambah.php :


<?php
include 'lib/library.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nis = $_POST['nis'];
    $nama_lengkap = $_POST['nama_lengkap'];
    $jenis_kelamin = $_POST['jenis_kelamin'];
    $kelas = $_POST['kelas'];
    $jurusan = $_POST['jurusan'];
    $alamat = $_POST['alamat'];
    $gol_darah = $_POST['gol_darah'];
    $nama_ortu = $_POST['nama_ortu'];

    $sql = "INSERT INTO siswa (nis,nama_lengkap,jenis_kelamin,kelas,jurusan,alamat,gol_darah,nama_ortu) VALUES ('$nis','$nama_lengkap','$jenis_kelamin','$kelas','$jurusan','$alamat','$golongan_darah','$ibu_kandung')";

    $mysqli->query($sql) or die($mysqli->error);

    header("location:index.php");
}

include 'views/v_tambah.php';
?>

Syntax edit.php :


<?php

include 'lib/library.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nis = $_POST['nis'];
    $nama_lengkap = $_POST['nama_lengkap'];
    $jenis_kelamin = $_POST['jenis_kelamin'];
    $kelas = $_POST['kelas'];
    $jurusan = $_POST['jurusan'];
    $alamat = $_POST['alamat'];
    $gol_darah = $_POST['gol_darah'];
    $nama_ortu = $_POST['nama_ortu'];

    $sql = "UPDATE siswa SET
                nama_lengkap = '$nama_lengkap',
                jenis_kelamin = '$jenis_kelamin',
                kelas = '$kelas',
                jurusan = '$jurusan',
                alamat = '$alamat',
                gol_darah = '$gol_darah',
                nama_ortu = '$nama_ortu'
            WHERE nis = $nis;
            ";

    $mysqli->query($sql) or die($mysqli->error);

    header('location: index.php');
}

$nis = $_GET['nis'];

if (empty($nis)) header('location: index.php');

$sql = "SELECT * FROM siswa WHERE nis = '$nis'";
$query = $mysqli->query($sql);
$siswa = $query->fetch_array();

if (empty($siswa)) header('location: index.php');

include 'views/v_tambah.php';
?>

Syntax delete.php :


<?php
include 'lib/library.php';
$nis = $_GET['nis'];

$sql = "DELETE FROM siswa WHERE nis = $nis";

$mysqli->query($sql) or die($mysqli->error);

header('location: index.php');
?>

Hasil :




Previous Post
Next Post

0 komentar: