Selasa, 14 Juni 2022

Membuat CRUD dengan PHP

 index.php


<?php 
    //mengarahkan ke file login.php
    header("location:login.php");
?>

koneksi.php

<?php 
    class koneksi {
        public function get_koneksi() {
            $conn = mysqli_connect("localhost", "root", "", "namadatabase");

            if(mysqli_connect_errno()) {
                echo "Gagal Koneksi ke Database: " . mysqli_connect_error();
            }
            return $conn;
        }
    }
    $konek = new koneksi();
    $koneksi = $konek->get_koneksi();
?>

login.php

<?php
    //import file koneksi.php
    include "koneksi.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
</head>
<body>
    <h2>LOGIN</h2>
    <?php 
if(isset($_GET['pesan'])){
if($_GET['pesan'] == "gagal"){
echo "Login gagal! username dan password salah!";
}else if($_GET['pesan'] == "logout"){
echo "Anda telah berhasil logout";
}else if($_GET['pesan'] == "belum_login"){
echo "Anda harus login untuk mengakses halaman admin";
}
}
?>
    <form method="post" action="cek_login.php">
        <table>
            <tr>
                <td>username</td>
                <td>:</td>
                <td><input type="text" name="username" placeholder="Masukkan username"></td>
            </tr>
            <tr>
                <td>password</td>
                <td>:</td>
                <td><input type="password" name="password" placeholder="Masukkan password"></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td><input type="submit" value="LOGIN"></td>
            </tr>
        </table>
    </form>
</body>
</html>

cek_login.php

//cek_login.php
<?php
// mengaktifkan session php
session_start();
 
// menghubungkan dengan koneksi
include 'koneksi.php';
 
// menangkap data yang dikirim dari form
$username = $_POST['username'];
$password = $_POST['password'];

// menyeleksi data admin dengan username dan password yang sesuai
$data = mysqli_query($koneksi,"select * from tbl_user where username='$username' and password='$password'");

// menghitung jumlah data yang ditemukan
$cek = mysqli_num_rows($data);

if($cek > 0){
$_SESSION['username'] = $username;
$_SESSION['status'] = "login";
header("location:admin_dashboard.php");
}else{
header("location:login.php?pesan=gagal");
}
?>

admin_dashboard.php

<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<h1>  Halaman Dashboard </h1>
<hr>
<ul>
<li><a href="dosen_list.php">Data Dosen</a></li>
<li><a href="#">Data Mata Kuliah</a></li>
<li><a href="#">Data Jadwal</a></li>
</ul>
</body>
</html>

dosen_add.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FORM TAMBAH DOSEN</title>
</head>
<body>
    <h2>Tambah Dosen</h2><br>
    <form method="post" action="aksi_add_dosen.php">
    <table>
    <tr>
        <td><label for="kd_dosen" >kode dosen</label></td>
        <td>:</td>
        <td><input type="text" name="kd_dosen" placeholder="masukkan kode dosen" required></td>
    </tr>
    <tr>
        <td><label for="nm_dosen" >nama dosen</label></td>
        <td>:</td>
        <td><input type="text" name="nm_dosen" placeholder="masukkan nama dosen" required></td>
    </tr>
    <tr>
        <td><label for="alamat" >Alamat Dosen</label></td>
        <td>:</td>
        <td> <textarea name="alamat" id="alamat" cols="30" rows="10"></textarea> </td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td><input type="submit" value="SIMPAN"> <input type="button" value="BATAL" onclick="window.location.href='dosen_list.php'"></td>
    </tr>
    </table>
    </form>
</body>
</html>

aksi_add_dosen.php

<?php
// koneksi database
include "koneksi.php";
 
// menangkap data yang di kirim dari form
$kd_dosen       = $_POST['kd_dosen'];
$nm_dosen       = $_POST['nm_dosen'];
$alamat         = $_POST['alamat'];

// menginput data ke database
mysqli_query($koneksi,"INSERT INTO tbl_dosen VALUES('$kd_dosen','$nm_dosen','$alamat')");


// mengalihkan halaman kembali ke dosen.php
header("location:dosen_list.php");

?>

dosen_list.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DATA DOSEN</title>
</head>
<body>
    <h2>DATA DOSEN</h2>
    <hr>
    <table>
        <tr>
            <td> <a href="dosen_add.php"> <button> + Tambah Data Dosen</button> </a> </td>
        </tr>
    </table>
    <br>
    <table border="1">
        <tr>
            <th>no</th>
            <th>kode dosen</th>
            <th>nama dosen</th>
            <th>alamat</th>
            <th>aksi</th>
        </tr>

        <?php
        include "koneksi.php"; 
        $no = 1;
        $sql = mysqli_query($koneksi, "select * from tbl_dosen");
        while($data = mysqli_fetch_array($sql)){
        ?>
            <tr>
                <td><?php echo $no++; ?></td>
                <td><?php echo $data['kd_dosen']; ?></td>
                <td><?php echo $data['nm_dosen']; ?></td>
                <td><?php echo $data['alamat']; ?></td>
                <td>
                    <a href="dosen_edit.php?id=<?php echo $data['kd_dosen'];?> ">Edit</a>
                    <a href="dosen_delete.php?kd_dosen=<?php echo $data['kd_dosen'];?> ">Hapus</a>
                </td>
            </tr>
        <?php
        }
        ?>
    </table>
    <br><br>
    <table>
    <tr>
        <a href="admin_dashboard.php"> <button>Kembali Ke Menu Utama</button> </a>
    </tr>
    </table>
</body>
</html>

dosen_delete.php

<?php
include 'koneksi.php';
 
 $kd_dosen = $_GET['kd_dosen'];

 mysqli_query($koneksi, "Delete from tbl_dosen where kd_dosen=$kd_dosen");

 header("location:dosen_list.php");

 ?>

dosen_edit.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>EDIT DOSEN</title>
</head>
<body>
    <h2>Edit Dosen</h2><br>

<?php
include 'koneksi.php';
$id = $_GET['id'];
$sqr = mysqli_query($koneksi, "SELECT * FROM tbl_dosen where kd_dosen = $id");
while ($data = mysqli_fetch_array($sqr)){
?>
<form method="post" action="aksi_edit_dosen.php">
    <table>
    <tr>
        <td><label for="kd_dosen" >kode dosen</label></td>
        <td>:</td>
        <td><input type="text" name="kd_dosen" value="<?php echo $data ['kd_dosen'];?>"></td>
    </tr>
    <tr>
        <td><label for="nm_dosen" >nama dosen</label></td>
        <td>:</td>
        <td><input type="text" name="nm_dosen" value="<?php echo $data ['nm_dosen'];?> "></td>
    </tr>
    <tr>
        <td><label for="alamat" >Alamat Dosen</label></td>
        <td>:</td>
        <td> <textarea name="alamat" id="alamat" cols="30" rows="10"><?php echo $data ['alamat'];?> </textarea> </td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td><input type="submit" value="SIMPAN"> <input type="button" value="BATAL" onclick="window.location.href='dosen_list.php'"></td>
    </tr>
</table>
</form>
<?php
}
?>
</body>
</html>

aksi_edit_dosen.php

<?php
// koneksi database
include "koneksi.php";
 
// menangkap data yang di kirim dari form
$kd_dosen       = $_POST['kd_dosen'];
$nm_dosen       = $_POST['nm_dosen'];
$alamat         = $_POST['alamat'];

// menginput data ke database
mysqli_query($koneksi,"INSERT INTO tbl_dosen VALUES('$kd_dosen','$nm_dosen','$alamat')");


// mengalihkan halaman kembali ke dosen.php
header("location:dosen_list.php");

?>