Professional Documents
Culture Documents
Laporan-Tugas3 - PHP-MySQL - Hendri Yunus Wijaya - Fauzia 'Uddin - M.Arifin Ilham - Zainal Ilmi
Laporan-Tugas3 - PHP-MySQL - Hendri Yunus Wijaya - Fauzia 'Uddin - M.Arifin Ilham - Zainal Ilmi
Disusun Oleh :
Mata Kuliah :
Pemrograman Web 2 (ABKC6404)
Dosen Pengampu :
Dr. Harja Santana Purba, M.Kom.
i
DAFTAR ISI
PEMBAHASAN....................................................................................................................... 1
A. Koneksi PHP DAN MySQL ............................................................................................... 1
1) Source Code koneksi : .................................................................................................... 1
B. Form Handling CRUD........................................................................................................ 1
1) Form Handling Read : ..................................................................................................... 1
2) Form Handling Hapus: .................................................................................................... 2
3) Form Handling Tambah : ................................................................................................ 2
4) Form Handling Edit : ...................................................................................................... 3
C. Kode PHP CRUD ............................................................................................................... 4
1) Source Code Tampilan Utama : ...................................................................................... 4
2) Source Code Tampilan Tambah : ................................................................................... 7
3) Source Code Tampilan Edit : .......................................................................................... 9
4) Source Code Tampilan Hapus : .................................................................................... 12
ii
PEMBAHASAN
A. Koneksi PHP DAN MySQL
if ($conn->connect_error) {
die("Koneksi gagal: " . $conn->connect_error);
}
?>
?>
1
2) Form Handling Hapus:
<?php
include('koneksi.php');
?>
3) Form Handling Tambah :
<?php
include('koneksi.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id_dokter = $conn->real_escape_string($_POST["id_dokter"]);
$nama_dokter = $conn->real_escape_string($_POST["nama_dokter"]);
$jenis_kelamin = $conn->real_escape_string($_POST["jenis_kelamin"]);
$keahlian = $conn->real_escape_string($_POST["keahlian"]);
$nomer_telpon = $conn->real_escape_string($_POST["nomer_telpon"]);
2
4) Form Handling Edit :
<?php
include('koneksi.php');
$id = $_GET['id'];
$sql = "SELECT * FROM dokter WHERE id_dokter='$id'";
$result = mysqli_query($conn, $sql);
if (!$result) {
die("Error: " . mysqli_error($conn));
}
$row = mysqli_fetch_assoc($result);
if (!$row) {
die("Data not found!");
}
$nama = $row['nama_dokter'];
$jenis_kelamin = $row['jenis_kelamin'];
$keahlian = $row['keahlian'];
$no_hp = $row['no_telpon'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nama = $_POST['nama'];
$jenis_kelamin = $_POST['jenis_kelamin'];
$keahlian = $_POST['keahlian'];
$no_hp = $_POST['no_hp'];
mysqli_close($conn);
?>
3
C. Kode PHP CRUD
4
.dokter_table tbody tr:hover td {
background-color: #ffffa2;
border-color: #ffff0f;
transition: all .2s;
}
.dokter_table tfoot th {
background-color: #e5f5ff;
}
.dokter_table tfoot th:first-child {
text-align: left;
}
.dokter_table tbody td:empty {
background-color: #ffcccc;
}
a {
text-decoration: none;
color: white;
}
.button_tambah {
background-color: rgb(39, 158, 39);
color: white;
padding: 10px 10px;
border: none;
border-radius: 10px;
cursor: pointer;
}
.button_tambah:hover {
background-color: rgb(13, 189, 13);
}
.button_hapus {
background-color: rgb(200, 0, 0);
color: white;
padding: 10px 10px;
border: none;
border-radius: 10px;
cursor: pointer;
}
.button_hapus:hover {
background-color: rgb(254, 43, 43);
}
</style>
</head>
<body>
<div class="center-table">
<table class="dokter_table">
<caption class="title">Tabel Data Dokter</caption>
<a href="tambah.php" class="tambah"><button
class="button_tambah">Tambah Dokter</button></a>
5
<thead>
<tr>
<th>ID DOKter</th>
<th>Nama</th>
<th>Jenis Kelamin</th>
<th>Keahlian</th>
<th>NO Telepon</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
include('koneksi.php');
$sql = "SELECT * FROM dokter";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>{$row['id_dokter']}</td>";
echo "<td>{$row['nama_dokter']}</td>";
echo "<td>{$row['jenis_kelamin']}</td>";
echo "<td>{$row['keahlian']}</td>";
echo "<td>{$row['no_telpon']}</td>";
echo "<td><button class='button_tambah'><a
href='edit.php?id={$row['id_dokter']}'>Edit</a>
</button><button class='button_hapus'><a
href='hapus.php?id={$row['id_dokter']}' onclick='return confirm(\"Apakah Anda
yakin ingin menghapus dokter ini?\")'>Hapus</a></button></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</body>
</html>
Output :
6
2) Source Code Tampilan Tambah :
<?php
include('koneksi.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id_dokter = $conn->real_escape_string($_POST["id_dokter"]);
$nama_dokter = $conn->real_escape_string($_POST["nama_dokter"]);
$jenis_kelamin = $conn->real_escape_string($_POST["jenis_kelamin"]);
$keahlian = $conn->real_escape_string($_POST["keahlian"]);
$nomer_telpon = $conn->real_escape_string($_POST["nomer_telpon"]);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tambah Dokter</title>
<style>
* {
font-family: monospace;
}
h2 {
text-align: center;
}
body {
padding: 20px;
}
form {
margin: 0 auto;
7
width: 700px;
}
input {
width: 100%;
padding: 8px;
margin: 5px 0;
box-sizing: border-box;
}
.submit {
background-color: rgb(39, 158, 39);
color: white;
margin-top: 20px;
border: none;
cursor: pointer;
padding: 10px;
}
.submit:hover {
background-color: rgb(13, 189, 13);
}
</style>
</head>
<body>
<h2>Tambah Dokter</h2>
<form action="" method="post">
<label for="id">ID Dokter:</label>
<input type="text" id="id" name="id_dokter" placeholder="Masukkan ID"
required>
<label for="keahlian">Keahlian:</label>
<input type="text" id="keahlian" name="keahlian" placeholder="Masukkan
Keahlian" required>
8
</form>
</form>
</body>
</html>
Output :
$id = $_GET['id'];
$sql = "SELECT * FROM dokter WHERE id_dokter='$id'";
$result = mysqli_query($conn, $sql);
if (!$result) {
die("Error: " . mysqli_error($conn));
}
$row = mysqli_fetch_assoc($result);
if (!$row) {
die("Data not found!");
9
}
$nama = $row['nama_dokter'];
$jenis_kelamin = $row['jenis_kelamin'];
$keahlian = $row['keahlian'];
$no_hp = $row['no_telpon'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nama = $_POST['nama'];
$jenis_kelamin = $_POST['jenis_kelamin'];
$keahlian = $_POST['keahlian'];
$no_hp = $_POST['no_hp'];
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Form</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
form {
width: 300px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
10
label {
font-weight: bold;
}
input[type="text"] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 5px;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
11
<input type="submit" value="Submit">
</form>
</body>
</html>
Output :
?>
12
Output :
Sebelum di hapus :
Sesudah di hapus :
13