0% found this document useful (0 votes)
11 views11 pages

Step by Step CRUD Dan Session

The document outlines a step-by-step guide to create a CRUD application using PHP and MariaDB, including setting up a database, creating tables for novels and users, and implementing user authentication with session management. It provides code snippets for connecting to the database, adding, editing, and deleting novel entries, as well as user login and logout functionalities. The document is structured to guide the user through each stage of development with clear instructions and examples.

Uploaded by

naufalmdzaky17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Step by Step CRUD Dan Session

The document outlines a step-by-step guide to create a CRUD application using PHP and MariaDB, including setting up a database, creating tables for novels and users, and implementing user authentication with session management. It provides code snippets for connecting to the database, adding, editing, and deleting novel entries, as well as user login and logout functionalities. The document is structured to guide the user through each stage of development with clear instructions and examples.

Uploaded by

naufalmdzaky17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Step By Step CRUD + Session

Buat Database dan Tabel

MariaDB [db_koran]> create database db_novel;


Query OK, 1 row affected (0.001 sec)

MariaDB [db_koran]> use db_novel;


Database changed
MariaDB [db_novel]> create table novel
-> ( no int primary key auto_increment,
-> judul varchar(50) not null,
-> pengarang varchar(50) not null,
-> harga int not null);
Query OK, 0 rows affected (0.047 sec)

MariaDB [db_novel]> insert into novel


-> values
-> (1,'selena','tere liye',80000),
-> (2,'hujan','tere liye',100000);
Query OK, 2 rows affected (0.008 sec)
Records: 2 Duplicates: 0 Warnings: 0

MariaDB [db_novel]> create table users (


-> id int primary key auto_increment,
-> username varchar(50) not null unique,
-> password varchar(255) not null);
Query OK, 0 rows affected (0.049 sec)

MariaDB [db_novel]> insert into users values (1,'admin',md5('admin'));


Query OK, 1 row affected (0.008 sec)

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;
}

$sql = "SELECT * FROM novel";


$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>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>

<?php while($novel = mysqli_fetch_assoc($query)) { ?>


<tr>
<td><?= $novel['no'] ?></td>
<td><?= $novel['judul'] ?></td>
<td><?= $novel['pengarang'] ?></td>
<td><?= $novel['harga'] ?></td>
<td>
<a href="edit.php?no=<?= $novel['no'] ?>">Edit</a>
<a href="hapus.php?no=<?= $novel['no'] ?>">Hapus</a>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
Buat tambah.php
<!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>Tambah</h1>
<form action="proses_tambah.php" method="get">
<label for="">Judul</label><br>
<input type="text" name="judul" id=""><br>

<label for="">Pengarang</label><br>
<input type="text" name="pengarang" id=""><br>

<label for="">Harga</label><br>
<input type="number" name="harga" id=""><br>

<input type="submit" value="Simpan">


</form>
</body>
</html>

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'];

$sql = "INSERT INTO novel (judul,pengarang,harga) VALUES


('$judul','$pengarang','$harga')";
$query = mysqli_query($koneksi, $sql);

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'];

$sql = "DELETE FROM novel WHERE no = '$no' ";


$query = mysqli_query($koneksi, $sql);

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);

while ($novel = mysqli_fetch_assoc($query)) { ?>


?>

<!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>

<input type="submit" value="Update">


</form>
</body>
</html>

<?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'];

$sql = "UPDATE novel SET judul = '$judul',


pengarang = '$pengarang',
harga = '$harga'
WHERE no = '$no' ";
$query = mysqli_query($koneksi,$sql);
if ($query) {
header("location:index.php?edit=sukses");
exit;
} else {
header("location:index.php?edit=gagal");
exit;
}
Buat login.php
<!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>Login</h1>
<form action="proses_login.php" method="post">
<label for="">Username</label><br>
<input type="text" name="username" id=""><br>

<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'];

$sql = "SELECT * FROM users WHERE username = '$username' AND


password = md5('$password') ";
$query = mysqli_query($koneksi, $sql);

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;
?>

You might also like