Membuat CRUD dengan PHP
Cara membuat CRUD dengan PHP
Pastikan sudah mengaktifkan XAMPP, Lalu buat folder baru di folder XAMPP -> htdocs dan isikan script dibawah ini :
.htaccess
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^([0-9a-zA-Z-]+)/?/?$ /login_php/index.php?page=$1
index.php
<?php
session_start();
include "koneksi.php";
if (isset($_SESSION['username'])==''){
include "login.php";
}else{
include "konten.php";
}
?>
koneksi.php
<?php
$koneksi = mysqli_connect("localhost","root","","siswa_smk");
// Cek koneksi
if (mysqli_connect_errno()){
echo "Koneksi database gagal : " . mysqli_connect_error();
}
?>
konten.php
<?php
if (!isset($_GET['page'])){
echo "Selamat, anda berhasil login
$_SESSION[username]!";
echo "<br>";
echo "<a href=http://localhost/login_php/siswa>Data Siswa</a>";
echo "<br>";
echo "<a href=logout.php>Logout</a>";
}elseif ($_GET['page'] == 'siswa'){
include "siswa.php";
}else{
echo "Maaf, halaman tidak ditemukan!";
}
?>
login.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>Document</title>
</head>
<body>
<h1>Login</h1>
<form action="" method="POST">
<label>Username</label><br>
<input type="text" name="username"><br>
<label>Password</label><br>
<input type="password" name="password"><br>
<button type="submit" name="login">Log in</button>
<p class="login-register-text">Anda belum punya akun? <a href="register.php">Register</a></p>
</form>
<?php
include "koneksi.php";
if (isset($_POST['login'])){
$user = $_POST['username'];
$pass = md5($_POST['password']);
$login=mysqli_query($koneksi, "SELECT * FROM user
WHERE username='$user' AND password='$pass'");
$cocok=mysqli_num_rows($login);
$r=mysqli_fetch_array($login);
if ($cocok > 0){
$_SESSION['username'] = $r['username'];
header('location:index.php');
}else{
echo "<script>window.alert('Maaf, Anda Tidak Memiliki akses');
window.location=('index.php')</script>";
}
}
?>
</body>
</html>
logout.php
<?php
session_start();
session_destroy();
header('Location:index.php');
die();
?>
siswa.php
<?php
if (!isset($_GET['aksi'])){
?>
<a type="button" href="index.php?page=siswa&aksi=tambah">Tambah Siswa</a>
<table>
<thead>
<tr>
<th>No</th>
<th>NIS</th>
<th>Nama Siswa</th>
<th>Jenis Kelamin</th>
<th>Alamat</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$siswa=mysqli_query($koneksi, "SELECT * FROM siswa");
$no = 1;
while ($data = mysqli_fetch_array($siswa)){
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $data['nis']; ?></td>
<td><?php echo $data['nama_siswa']; ?></td>
<td><?php echo $data['jenis_kelamin']; ?></td>
<td><?php echo $data['alamat']; ?></td>
<td><a href="index.php?page=siswa&aksi=edit&id=<?php echo $data['id_siswa'] ?>">Edit</a> |
<a href="index.php?page=siswa&aksi=hapus&id=<?php echo $data['id_siswa'] ?>">Hapus</a></td>
</tr>
<?php
$no++;
}
?>
</tbody>
</table>
<?php
}elseif ($_GET['aksi']=='tambah'){
?>
<form action='' method="POST" enctype='multipart/form-data'>
<label>NIS</label>
<input type="text" name="a"><br>
<label>Nama Siswa</label>
<input type="text" name="b"><br>
<label>Jenis Kelamin</label>
<input type="text" name="c"><br>
<label>Alamat</label>
<input type="text" name="d"><br>
<label>Foto Siswa</label>
<input type="file" name="e"><br>
<button type="submit" name="simpan">Simpan</button>
<?php
if (isset($_POST['simpan'])){
$dir_foto = 'foto/';
$filename = basename($_FILES['e']['name']);
$uploadfile = $dir_foto . $filename;
if ($filename != ''){
if (move_uploaded_file($_FILES['e']['tmp_name'], $uploadfile)) {
mysqli_query($koneksi,"INSERT INTO siswa (nis, nama_siswa, jenis_kelamin, alamat, foto_siswa)
VALUES('$_POST[a]','$_POST[b]','$_POST[c]','$_POST[d]','$filename')");
echo "<script>window.alert('Sukses Menambahkan Data Siswa.');
window.location='siswa'</script>";
}else{
echo "<script>window.alert('Gagal Menambahkan Data Siswa.');
window.location='index.php?page=siswa&aksi=tambah'</script>";
}
}else{
mysqli_query($koneksi,"INSERT INTO siswa (nis, nama_siswa, jenis_kelamin, alamat)
VALUES('$_POST[a]','$_POST[b]','$_POST[c]','$_POST[d]')");
echo "<script>window.alert('Sukses Menambahkan Data Siswa .');
window.location='siswa'</script>";
}
}
}elseif ($_GET['aksi']=='edit'){
$siswa = mysqli_query($koneksi, "SELECT * FROM siswa where id_siswa='$_GET[id]'");
$data = mysqli_fetch_array($siswa);
?>
<form action='' method="POST" enctype='multipart/form-data'>
<label>NIS</label>
<input type="text" name="a" value="<?php echo $data['nis']; ?>"><br>
<label>Nama Siswa</label>
<input type="text" name="b" value="<?php echo $data['nama_siswa']; ?>"><br>
<label>Jenis Kelamin</label>
<input type="text" name="c" value="<?php echo $data['jenis_kelamin']; ?>"><br>
<label>Alamat</label>
<input type="text" name="d" value="<?php echo $data['alamat']; ?>"><br>
<label>Foto Siswa</label>
<input type="file" name="e"><br>
<button type="submit" name="update">Simpan</button>
<?php
if (isset($_POST['update'])){
$dir_foto = 'foto/';
$filename = basename($_FILES['e']['name']);
$uploadfile = $dir_foto . $filename;
if ($filename != ''){
if (move_uploaded_file($_FILES['e']['tmp_name'], $uploadfile)) {
mysqli_query($koneksi,"UPDATE siswa SET nis = '$_POST[a]',
nama_siswa = '$_POST[b]',
jenis_kelamin = '$_POST[c]',
alamat = '$_POST[d]',
foto_siswa = '$filename' where id_siswa = '$_GET[id]'");
echo "<script>window.alert('Sukses Update Data Siswa.');
window.location='siswa'</script>";
}else{
echo "<script>window.alert('Gagal Update Data Siswa.');
window.location='index.php?page=siswa&aksi=tambah'</script>";
}
}else{
mysqli_query($koneksi,"UPDATE siswa SET nis = '$_POST[a]',
nama_siswa = '$_POST[b]',
jenis_kelamin = '$_POST[c]',
alamat = '$_POST[d]',
foto_siswa = '$filename' where id_siswa =
'$_GET[id]'");
echo "<script>window.alert('Sukses Update Data Siswa .');
window.location='siswa'</script>";
}
}
}elseif ($_GET['aksi']=='hapus'){
mysqli_query($koneksi, "DELETE FROM siswa where id_siswa='$_GET[id]'");
echo "<script>window.alert('Data Siswa Berhasil Di Hapus.');
window.location='siswa'</script>";
}
?>
Setelah download dan import File My SQL dibawah ini :
Untuk mengetesnya harus login dulu seperti gambar dibawah ini :
Setelah itu klik "Data Siswa", Disitu anda bisa menambah dan mengedit data siswa
Posting Komentar untuk "Membuat CRUD dengan PHP"