Step by Step CRUD Dan Session
Step by Step CRUD Dan Session
MariaDB [db_novel]>
Buat koneksi.php
<?php
$server = "localhost";
$username = "root";
$password = "123456";
$database = "db_novel";
$koneksi = mysqli_connect($server,$username,$password,$database);
if ($koneksi) {
echo "berhasil terkoneksi";
} else {
echo "gagal terkoneksi";
}
Buat index.php
( kode berwarna kuning ditambahkan setelah login dan session selesai)
<?php
session_start();
include "koneksi.php";
if(!isset($_SESSION['username'])) {
header("location:login.php?pesan=logindulu");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Novel</h1>
<a href="tambah.php">Tambah</a><br><br>
<table border="1">
<tr>
<th>No</th>
<th>Judul</th>
<th>Pengarang</th>
<th>Harga</th>
<th>Aksi</th>
</tr>
<label for="">Pengarang</label><br>
<input type="text" name="pengarang" id=""><br>
<label for="">Harga</label><br>
<input type="number" name="harga" id=""><br>
Buat proses_tambah.php
<?php
session_start();
include "koneksi.php";
if(!isset($_SESSION['username'])) {
header("location:login.php?pesan=logindulu");
exit;
}
$judul = $_GET['judul'];
$pengarang = $_GET['pengarang'];
$harga = $_GET['harga'];
if ($query) {
header("location:index.php?simpan=sukses");
exit;
} else {
header("location:index.php?simpan=gagal");
exit;
}
Buat hapus.php
<?php
session_start();
include "koneksi.php";
if(!isset($_SESSION['username'])) {
header("location:login.php?pesan=logindulu");
exit;
}
$no = $_GET['no'];
if ($query) {
header("location:index.php?hapus=sukses");
exit;
} else {
header("location:index.php?hapus=gagal");
}
Buat edit.php
<?php
session_start();
include "koneksi.php";
if(!isset($_SESSION['username'])) {
header("location:login.php?pesan=logindulu");
exit;
}
$no = $_GET['no'];
$sql = "SELECT * FROM novel WHERE no = '$no' ";
$query = mysqli_query($koneksi, $sql);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Edit</h1>
<form action="proses_edit.php" method="get">
<input type="hidden" name="no" value="<?= $novel['no'] ?>">
<label for="">Judul</label><br>
<input type="text" name="judul" id="" value="<?= $novel['judul'] ?>"><br>
<label for="">Pengarang</label><br>
<input type="text" name="pengarang" id="" value="<?=
$novel['pengarang'] ?>"><br>
<label for="">Harga</label><br>
<input type="number" name="harga" id="" value="<?= $novel['harga'] ?>"><br>
<?php } ?>
Buat proses_edit.php
<?php
session_start();
include "koneksi.php";
if(!isset($_SESSION['username'])) {
header("location:login.php?pesan=logindulu");
exit;
}
$no = $_GET['no'];
$judul = $_GET['judul'];
$pengarang = $_GET['pengarang'];
$harga = $_GET['harga'];
<label for="">Password</label><br>
<input type="password" name="password" id=""><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Buat proses_login.php
<?php
session_start();
include "koneksi.php";
$username = $_POST['username'];
$password = $_POST['password'];
if (mysqli_num_rows($query) == 1) {
$_SESSION['username'] = $username;
header("location:index.php?login=sukses");
exit;
} else {
header("location:login.php?login=gagal");
exit;
}
Buat logout.php
<?php
session_start();
session_unset();
session_destroy();
header("location:login.php?logout=sukses");
exit;
?>