Membuat CRUD Dengan PHP Dan MySQLi - membuat edit data

 Assalamualaikum Wr. Wb.

Ahmad Dindan Romadhoni

    Seperti yang kita lihat pada file siswa.php kita telah membuat tombol/link edit.Langkah pertama yang kita akan lakuakn untukmembuat edit adalah membuat file esiswa.php, Pada file esiswa.php  ini kita akan membuat form edit untuk  data siswa

esiswa.php

<!doctype html>

<html lang="en">

  <head>

    <!-- Required meta tags -->

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">

<link rel="stylesheet" type="text/css" href="ff/css/all.min.css">

<link rel="stylesheet" type="text/css" href="style.css">

    <title>Hello, world!</title>

  </head>

  <body>

<div class="container">

    <center><h1>Form Edit Data</h1></center>

<div class="card">

    <div class="card-header bg-primary">

        Edit Data Siswa

    </div>

    <div class="card-body">

        <?php

    include 'koneksi.php';

    $id = $_GET['id'];

    $data = mysqli_query($koneksi,"select * from siswa where id='$id'");

    while($d = mysqli_fetch_array($data)){

        ?>

        <form method="post" action="usiswa.php">

        <label class="nama">Nama Siswa</label>

        <input type="hidden" name="id" value="<?php echo $d['id']; ?>">

        <input type="text" class="form-control" name="nama" value="<?php echo $d['nama']; ?>">


    <label class="nama">Alamat</label>

    <input type="text" class="form-control" name="alamat" value="<?php echo $d['alamat']; ?>">


        <label class="nama">NIS</label>

        <input type="text" class="form-control" name="NIS" value="<?php echo $d['NIS']; ?>">


    <label class="nama">TTL</label>

    <input type="date" class="form-control" name="TTL" value="<?php echo $d['TTL']; ?>">

<input type="submit" value="Simpan" class="btn btn-primary mt-3"></input>     

</form>

<?php

}

?>

    </div>

</div>

</div>

<!-- Optional JavaScript; choose one of the two! -->

<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>

<script type="text/javascript" src="admin.js"></script>

<!-- Option 2: Separate Popper and Bootstrap JS -->

 <!-- <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" integrity="sha384-+YQ4JLhjyBLPDQt//I+STsc9iw4uQqACwlvpslubQzn4u2UU2UFM80nGisd026JF" crossorigin="anonymous"></script>-->

  </body>

</html>

Lalu kita buat satu file lagi yaitu usiswa.php

usiswa.php

<?php 

// koneksi database

include 'koneksi.php';


// menangkap data yang di kirim dari form

$id = $_POST['id'];

$nama = $_POST['nama'];

$alamat = $_POST['alamat'];

$NIS = $_POST['NIS'];

$TTL = $_POST['TTL'];

// update data ke database

mysqli_query($koneksi,"update siswa set nama='$nama', alamat='$alamat', NIS='$NIS' , TTL='$TTL' where id='$id'");

// mengalihkan halaman kembali ke index.php

header("location:siswa.php");

?>

Dan Hasilnya sebelum di edit:

Dan Sesudah Di Edit:
Sekian dari saya.Terima Kasih

0 Komentar