Skip to content

Commit

Permalink
tugas membuat crud dengan session
Browse files Browse the repository at this point in the history
  • Loading branch information
Nvrmre authored Oct 1, 2024
1 parent 54bb267 commit 2fd2edd
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 0 deletions.
90 changes: 90 additions & 0 deletions MSIB-crud/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* Reset margin dan padding */
body, h1, table {
margin: 0;
padding: 0;
}

/* Mengatur font dan warna dasar */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
}

/* Styling untuk header */
h1 {
text-align: center;
margin: 20px 0;
}

/* Styling untuk tabel */
table {
width: 80%;
margin: 20px auto;
border-collapse: collapse;
background-color: white;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

th, td {
padding: 12px;
text-align: left;
border: 1px solid #ddd;
}

th {
background-color: #4CAF50;
color: white;
}

/* Styling untuk link */
a {
color: #4CAF50;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* Styling untuk form */
form {
width: 80%;
margin: 20px auto;
background-color: white;
padding: 20px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

label {
display: block;
margin: 10px 0 5px;
}

input[type="text"],
input[type="email"] {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}

button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}

button:hover {
background-color: #45a049;
}

/* Styling untuk tautan kembali */
a.back-link {
display: block;
text-align: center;
margin: 20px 0;
}
37 changes: 37 additions & 0 deletions MSIB-crud/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
include 'src/koneksi.php';

$sql = "SELECT * FROM msib_data";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="css/style.css">
<head>
<title>Data</title>
</head>
<body>
<h1>Data</h1>
<a href="src/create.php">Tambah Data</a>
<table border="1">
<tr>
<th>ID</th>
<th>Nama</th>
<th>Email</th>
<th>Aksi</th>
</tr>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nama']; ?></td>
<td><?php echo $row['email']; ?></td>
<td>
<a href="src/update.php?id=<?php echo $row['id']; ?>">Edit</a>
<a href="src/delete.php?id=<?php echo $row['id']; ?>">Hapus</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
34 changes: 34 additions & 0 deletions MSIB-crud/src/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
include 'koneksi.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$nama = $_POST['nama'];
$email = $_POST['email'];

$sql = "INSERT INTO msib_data (nama, email) VALUES ('$nama', '$email')";

if ($conn->query($sql) === TRUE) {
$_SESSION['message'] = "Data berhasil ditambahkan!";
header('Location: ../index.php');
} else {
$_SESSION['message'] = "Error: " . $conn->error;
}
}
?>

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="../css/style.css">
<head>
<title>Tambah Data</title>
</head>
<body>
<h1>Tambah Data</h1>
<form method="POST" action="">
Nama: <input type="text" name="nama" required><br>
Email: <input type="email" name="email" required><br>
<input type="submit" value="Tambah">
</form>
<p><?php if(isset($_SESSION['message'])) echo $_SESSION['message']; ?></p>
</body>
</html>
14 changes: 14 additions & 0 deletions MSIB-crud/src/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
include 'koneksi.php';

$id = $_GET['id'];

$sql = "DELETE FROM msib_data WHERE id = $id";

if ($conn->query($sql) === TRUE) {
$_SESSION['message'] = "Data berhasil dihapus!";
header('Location:../index.php');
} else {
$_SESSION['message'] = "Error: " . $conn->error;
}
?>
18 changes: 18 additions & 0 deletions MSIB-crud/src/koneksi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$host = "localhost";
$username = "root";
$db = "msib";
$password = "";

$conn = mysqli_connect($host,$username,$password,$db);

// if ($conn->connect_error) {
// die("Koneksi gagal: " . $conn->connect_error);
// } else {
// echo "berhasil";
// }



?>
39 changes: 39 additions & 0 deletions MSIB-crud/src/update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
include 'koneksi.php';

$id = $_GET['id'];
$sql = "SELECT * FROM msib_data WHERE id = $id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$nama = $_POST['nama'];
$email = $_POST['email'];

$sql = "UPDATE msib_data SET nama='$nama', email='$email' WHERE id=$id";

if ($conn->query($sql) === TRUE) {
$_SESSION['message'] = "Data berhasil diupdate!";
header('Location: ../index.php');
} else {
$_SESSION['message'] = "Error: " . $conn->error;
}
}
?>

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="../css/style.css">
<head>
<title>Update Data</title>
</head>
<body>
<h1>Update Data</h1>
<form method="POST" action="">
Nama: <input type="text" name="nama" value="<?php echo $row['nama']; ?>" required><br>
Email: <input type="email" name="email" value="<?php echo $row['email']; ?>" required><br>
<input type="submit" value="Update">
</form>
<p><?php if(isset($_SESSION['message'])) echo $_SESSION['message']; ?></p>
</body>
</html>

0 comments on commit 2fd2edd

Please sign in to comment.