CODE.
CODING.
koneksi.php
<?php
$DB_HOST = "localhost";
$DB_USER = "root";
$DB_PASSWORD = "";
$DB_NAME = "fz2h";
$koneksi = mysqli_connect($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME);
if (!$koneksi) {
echo "Ada Error Kawan";
exit();
}
mysqli_set_charset($koneksi, "utf8");
21:20
create.php
<?php
include 'koneksi.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "INSERT INTO users (fullname, email, password) VALUES (?, ?, ?)";
$stmt = $koneksi->prepare($sql);
$stmt->bind_param("sss", $fullname, $email, $password);
if ($stmt->execute()) {
header('Location: index.php');
} else {
echo "Error : " . $stmt->error;
}
$stmt->close();
mysqli_close($koneksi);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Data</title>
</head>
<body>
<h1>Create Data</h1>
<form method="POST">
<div>
<label for="fullname">Full Name</label>
<input id="fullname" type="text" name="fullname">
</div>
<div>
<label for="email">Email</label>
<input id="email" type="text" name="email">
</div>
<div>
<label for="password">Password</label>
<input id="password" type="password" name="password">
</div>
<input type="file">
<button type="submit">Create</button>
</form>
</body>
</html>
20:36
update.php
<?php
include 'koneksi.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'];
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "UPDATE users SET fullname = ?, email = ?, password = ? WHERE id = ?";
$stmt = $koneksi->prepare($sql);
$stmt->bind_param("sssi", $fullname, $email, $password, $id);
if ($stmt->execute()) {
header('Location: index.php');
} else {
echo "Error : " . $stmt->error;
}
$stmt->close();
mysqli_close($koneksi);
}
$ids = $_GET['id'];
$sql = "SELECT * FROM users WHERE id = ?";
$stmt = $koneksi->prepare($sql);
$stmt->bind_param("i", $ids);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Data</title>
</head>
<body>
<h1>Update Data</h1>
<form method="POST">
<input type="hidden" name="id" value="<?= $ids ?>">
<div>
<label for="fullname">Full Name</label>
<input id="fullname" type="text" name="fullname" value="<?= $data["fullname"] ?>">
</div>
<div>
<label for="email">Email</label>
<input id="email" type="text" name="email" value="<?= $data["email"] ?>">
</div>
<div>
<label for="password">Password</label>
<input id="password" type="password" name="password" value="<?= $data["password"] ?>">
</div>
<button type="submit">Update</button>
</form>
</body>
</html>
22:23
read.php
<?php
include 'koneksi.php';
$id = $_GET['id'];
$sql = "SELECT * FROM users WHERE id = ?";
$stmt = $koneksi->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Read Data</title>
</head>
<body>
<h1>Read Data</h1>
<p>
Id : <?= $data["id"] ?>
</p>
<p>
Full Name : <?= $data["fullname"] ?>
</p>
<p>
Email : <?= $data["email"] ?>
</p>
<p>
Password : <?= $data["password"] ?>
</p>
</body>
</html>
22:23
delete.php
<?php
include 'koneksi.php';
$id = $_GET['id'];
$sql = "DELETE FROM users WHERE id = ?";
$stmt = $koneksi->prepare($sql);
$stmt->bind_param("i", $id);
if ($stmt->execute()) {
header("Location: index.php");
} else {
echo $koneksi->error;
}
$stmt->close();
mysqli_close($koneksi);
22:23
logout.php
<?php
session_start();
include 'koneksi.php';
if (isset($_SESSION['session_email'])) {
$email = $_SESSION['session_email'];
session_regenerate_id(true);
session_unset();
session_destroy();
header("Location: login.php");
exit();
}
header("Location: login.php");
exit();
21:52
login.php
<?php
include 'koneksi.php';
session_start();
$msg = '';
if (isset($_SESSION['session_email'])) {
header("Location: index.php");
exit();
}
if (isset($_POST['LOGIN'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE email = ?";
$stmt = $koneksi->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['session_email'] = $user['email'];
$_SESSION['session_role'] = $user['role'];
header('Location: index.php');
} else {
$msg = "Login Failed";
}
$stmt->close();
mysqli_close($koneksi);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body>
<div class="flex justify-center items-center h-screen">
<div class="w-full max-w-sm">
<img class="w-12 object-cover rounded-full mx-auto mb-4" src="img/logo.png" alt="">
<h1 class="text-xl font-bold text-center mb-8">Login Ke Akun Anda</h1>
<div class="bg-white shadow-lg p-10 rounded-lg">
<p class="text-red-400 text-center"><?= $msg ?></p>
<form method="POST">
<div class="mb-2">
<label class="block mb-1" for="email">Email</label>
<input class="border rounded-lg p-1 border-teal-100 w-full" id="email" type="text" name="email" required placeholder="Email">
</div>
<div class="mb-2">
<label class="block mb-1" for="password">Password</label>
<input class="border rounded-lg p-1 border-teal-100 w-full" id="password" type="password" name="password" required placeholder="****">
</div>
<button class="bg-green-400 w-full p-1 text-white rounded-lg mt-6" type="submit" name="LOGIN">Login</button>
</form>
</div>
</div>
</div>
</body>
</html>
21:31
index.php
<?php
include 'koneksi.php';
session_start();
if (!isset($_SESSION['session_email'])) {
header("Location: login.php");
exit();
}
$sql = "SELECT * FROM users";
$stmt = $koneksi->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$no = 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=`device-width`, initial-scale=1.0">
<title>Looping Data</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body x-data="{ modalTambah : false }">
<h2>
<?= $_SESSION['session_email'] ?>
<?= "<br>" ?>
<?= $_SESSION['session_role'] ?>
</h2>
<h1>Looping Data</h1>
<a href="logout.php">
Logout
</a>
<br>
<br>
<button @click="modalTambah = true">
Tambah
</button>
<br>
<br>
<table class="min-w-full divide-y divide-gray-300">
<thead class="bg-gray-50">
<tr class="divide-x divide-gray-200">
<th class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">No</th>
<th class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Full Name</th>
<th class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Email</th>
<th class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Action</th>
</tr>
</thead>
<tbody class="bg-white">
<?php if ($result->num_rows > 0): ?>
<?php while ($data = $result->fetch_assoc()): ?>
<tr class="divide-x divide-gray-200">
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
<?= $no++ ?>
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
<?= htmlspecialchars($data["fullname"]) ?>
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
<?= htmlspecialchars($data["email"]) ?>
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
<a href="read.php?id=<?= $data["id"] ?>">Read</a>
<a href="update.php?id=<?= $data["id"] ?>">Update</a>
<?php if ($_SESSION['session_role'] === 'admin') : ?>
<a href="delete.php?id=<?= $data["id"] ?>">Hapus</a>
<?php endif ?>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td>Tidak Ada Data</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<?php include 'modal.php' ?>
</body>
</html>
<?php
$stmt->close();
mysqli_close($koneksi);
?>
22:08
modal.php
<!-- Modal Tambah -->
<div x-show="modalTambah" class="fixed inset-0 z-50 bg-black/20">
<div class="flex justify-center items-center h-screen">
<div class="bg-white shadow-lg p-8 rounded-lg w-full max-w-sm">
<form method="POST">
<div class="mb-2">
<label class="block mb-1" for="fullname">Full Name</label>
<input class="border rounded-lg p-1 border-teal-100 w-full" id="fullname" type="text" name="fullname" required placeholder="Fullname">
</div>
<div class="mb-2">
<label class="block mb-1" for="email">Email</label>
<input class="border rounded-lg p-1 border-teal-100 w-full" id="email" type="text" name="email" required placeholder="Email">
</div>
<div class="mb-2">
<label class="block mb-1" for="password">Password</label>
<input class="border rounded-lg p-1 border-teal-100 w-full" id="password" type="password" name="password" required placeholder="****">
</div>
<div class="mb-2">
<label class="block mb-1" for="role">Role</label>
<select class="border rounded-lg p-1 border-teal-100 w-full" name="role" id="role">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="flex justify-between mt-8">
<button class="bg-gray-400 p-2 text-white rounded-lg mt-6 hover:cursor-pointer hover:bg-gray-600" @click="modalTambah = false">Batal</button>
<button class="bg-green-400 p-2 text-white rounded-lg mt-6 hover:cursor-pointer hover:bg-green-600" type="submit">Tambah</button>
</div>
</form>
</div>
</div>
</div>
22:08