Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Tugas 3: Mini Proyek Web Aplikasi PHP dan MySQL

Disusun Oleh :

Fauzia Uddin (2210131210006)


Hendri Yunus Wijaya (2210131210025)
M. Arifin Ilham (2210131110005)
Zainal Ilmi (2210131310011)

Mata Kuliah :
Pemrograman Web 2 (ABKC6404)

Dosen Pengampu :
Dr. Harja Santana Purba, M.Kom.

Novan Alkaf Bahraini Saputra, S.Kom., M.T

UNIVERSITAS LAMBUNG MANGKURAT


FAKULTAS KEGURUAN DAN ILMU PENDIDIKAN
PROGRAM STUDI PENDIDIKAN KOMPUTER
2024

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

1) Source Code koneksi :


<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "db_dokter";

$conn = new mysqli($servername, $username, $password, $database);

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

?>

B. Form Handling CRUD

1) Form Handling Read :


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

?>

1
2) Form Handling Hapus:
<?php
include('koneksi.php');

if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["id"])) {


$id = $conn->real_escape_string($_GET["id"]);
$sql = "DELETE FROM dokter WHERE id_dokter = '$id'";
if ($conn->query($sql)) {
echo "Berhasil Menghapus Data Dokter";
header("Location: read.php");
exit();
} else {
echo "Gagal Menghapus Data Dokter: " . $conn->error;
}
}

?>
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"]);

$sql = "INSERT INTO dokter (id_dokter, nama_dokter, jenis_kelamin,


keahlian, no_telpon)
VALUES ('$id_dokter', '$nama_dokter', '$jenis_kelamin',
'$keahlian', '$nomer_telpon')";

if ($conn->query($sql) === TRUE) {


echo "<center>Berhasil Menambahkan Data Dokter.</center>";
header("Location: read.php");
exit();
} else {
echo "<center>Gagal Menambahkan Data Dokter: " . $conn->error .
"</center>";
}
}
?>

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

$sql = "UPDATE dokter SET nama_dokter='$nama',


jenis_kelamin='$jenis_kelamin', keahlian='$keahlian', no_telpon='$no_hp' WHERE
id_dokter='$id'";
if (mysqli_query($conn, $sql)) {
header("Location: read.php");
exit();
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}

mysqli_close($conn);
?>

3
C. Kode PHP CRUD

1) Source Code Tampilan Utama :


<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Lucida Sans Unicode", "Lucida Grande", "Segoe Ui";
display: flex;
justify-content: center;
align-items: center;
height: 350px;
margin: 0;
}
.center-table {
width: 80%;
}
.dokter_table {
border-collapse: collapse;
font-size: 13px;
}
.dokter_table th,
.dokter_table td {
border: 1px solid #e1edff;
padding: 7px 17px;
}
.dokter_table .title {
caption-side: bottom;
margin-top: 12px;
}
.dokter_table thead th {
background-color: #508abb;
color: #FFFFFF;
border-color: #6ea1cc !important;
text-transform: uppercase;
}
.dokter_table tbody td {
color: #353535;
}
.dokter_table tbody td:first-child,
.dokter_table tbody td:last-child,
.dokter_table tbody td:nth-child(4) {
text-align: right;
}
.dokter_table tbody tr:nth-child(odd) td {
background-color: #f4fbff;
}

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

$sql = "INSERT INTO dokter (id_dokter, nama_dokter, jenis_kelamin,


keahlian, no_telpon)
VALUES ('$id_dokter', '$nama_dokter', '$jenis_kelamin',
'$keahlian', '$nomer_telpon')";

if ($conn->query($sql) === TRUE) {


echo "<center>Berhasil Menambahkan Data Dokter.</center>";
header("Location: read.php");
exit();
} else {
echo "<center>Gagal Menambahkan Data Dokter: " . $conn->error .
"</center>";
}
}
?>

<!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="nama">Nama Dokter:</label>


<input type="text" id="nama" name="nama_dokter" placeholder="Masukkan
Nama" required>

<label for="jenis_kelamin">Jenis Kelamin:</label>


<input type="text" id="jenis_kelamin" name="jenis_kelamin"
placeholder="Masukkan Jenis Kelamin" required>

<label for="keahlian">Keahlian:</label>
<input type="text" id="keahlian" name="keahlian" placeholder="Masukkan
Keahlian" required>

<label for="nomer_telpon">Nomor Telpon:</label>


<input type="text" id="nomer_telpon" name="nomer_telpon"
placeholder="Masukkan Nomor Telpon" required>

<input type="submit" value="Tambah Dokter" class="submit">

8
</form>

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

Output :

3) Source Code Tampilan 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!");

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

$sql = "UPDATE dokter SET nama_dokter='$nama',


jenis_kelamin='$jenis_kelamin', keahlian='$keahlian', no_telpon='$no_hp' WHERE
id_dokter='$id'";
if (mysqli_query($conn, $sql)) {
header("Location: read.php");
exit();
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}

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>

<h2 style="text-align: center;">Edit Form</h2>

<form action="#" method="POST">


<label for="id">ID:</label><br>
<input type="text" id="id" name="id" value="<?php echo $id; ?>"
readonly><br>
<label for="nama">Nama:</label><br>
<input type="text" id="nama" name="nama" value="<?php echo $nama; ?>"><br>
<label for="jenis_kelamin">Jenis Kelamin:</label><br>
<input type="text" id="jenis_kelamin" name="jenis_kelamin" value="<?php echo
$jenis_kelamin; ?>"><br>
<label for="keahlian">Keahlian:</label><br>
<input type="text" id="keahlian" name="keahlian" value="<?php echo
$keahlian; ?>"><br>
<label for="no_hp">Nomor HP:</label><br>
<input type="text" id="no_hp" name="no_hp" value="<?php echo $no_hp;
?>"><br><br>

11
<input type="submit" value="Submit">
</form>

</body>
</html>

Output :

4) Source Code Tampilan Hapus :


<?php
include('koneksi.php');

if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["id"])) {


$id = $conn->real_escape_string($_GET["id"]);
$sql = "DELETE FROM dokter WHERE id_dokter = '$id'";
if ($conn->query($sql)) {
echo "Berhasil Menghapus Data Dokter";
header("Location: read.php");
exit();
} else {
echo "Gagal Menghapus Data Dokter: " . $conn->error;
}
}

?>

12
Output :
Sebelum di hapus :

Sesudah di hapus :

13

You might also like