0% found this document useful (0 votes)
6 views8 pages

5a. Pengenalan Database MySQL Dan Proses CRUD

The document provides an introduction to MySQL databases and CRUD processes, detailing the structure of a database, and includes PHP code examples for database connection, reading, creating, updating, and deleting records. It outlines the necessary code for each operation, along with HTML for user interaction. The document serves as a practical guide for implementing a simple customer management system using PHP and MySQL.

Uploaded by

kingbataraa
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)
6 views8 pages

5a. Pengenalan Database MySQL Dan Proses CRUD

The document provides an introduction to MySQL databases and CRUD processes, detailing the structure of a database, and includes PHP code examples for database connection, reading, creating, updating, and deleting records. It outlines the necessary code for each operation, along with HTML for user interaction. The document serves as a practical guide for implementing a simple customer management system using PHP and MySQL.

Uploaded by

kingbataraa
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/ 8

MATERI 5: PENGENALAN DATABASE MYSQL DAN PROSES CRUD

DAFTAR ISI

Struktur Database ............................................................................................................................ 2

Isi tabel pelanggan ........................................................................................................................... 2

Koding db.php ................................................................................................................................. 2

Koding read.php .............................................................................................................................. 3

Koding create.php ........................................................................................................................... 4

Koding delete.php ........................................................................................................................... 6

Koding update.php .......................................................................................................................... 7

1
Iyus Supriadi, S.T., M.T. – [email protected]
MATERI 5: PENGENALAN DATABASE MYSQL DAN PROSES CRUD

Struktur Database

Isi tabel pelanggan

Koding db.php
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "toko_online";
try {
$conn = new PDO("mysql:host=$host;dbname=$database", $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
2
Iyus Supriadi, S.T., M.T. – [email protected]
MATERI 5: PENGENALAN DATABASE MYSQL DAN PROSES CRUD

} catch (PDOException $e) {


die("Koneksi gagal: " . $e->getMessage());
}

Kode diatas disimpan : C:\xampp\htdocs\progint2\minggu5\db.php

Koding read.php

<?php
require 'db.php';
$sql = "SELECT * FROM pelanggan";
$stmt = $conn->query($sql);
$pelanggan = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css"/>

<div class="box">

<nav class="breadcrumb" aria-label="breadcrumbs">


<ul>
<li class="is-active"><a href="#" aria-current="page">Home</a></li>
</ul>
</nav>

<h1 class="title">Pelanggan</h1>

<button class="button is-primary is-small"><a href="create.php">Tambah</a></button>

<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Nama</th>
<th>Email</th>
<th>Umur</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php foreach ($pelanggan as $p): ?>
<tr>
<td><?= $p["id"] ?></td>
<td><?= $p["nama"] ?></td>
<td><?= $p["email"] ?></td>
<td><?= $p["umur"] ?></td>
<td>
<button class="button is-warning is-small "><a href="update.php?id=<?= $p['id']
?>">Edit</a></button>

3
Iyus Supriadi, S.T., M.T. – [email protected]
MATERI 5: PENGENALAN DATABASE MYSQL DAN PROSES CRUD

<button class="button is-danger is-small "><a href="delete.php?id=<?= $p['id']


?>" onclick="return confirm('Hapus data?')">Hapus</a></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>

Kode diatas disimpan : C:\xampp\htdocs\progint2\minggu5\read.php

Bukan alamat di browser : http://localhost/progint2/minggu5/read.php


sehingg tampilannya sebagai berikut :

Koding create.php
<?php
require 'db.php';

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

$sql = "INSERT INTO pelanggan (nama, email, umur) VALUES (:nama, :email, :umur)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':nama', $nama);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':umur', $umur);
4
Iyus Supriadi, S.T., M.T. – [email protected]
MATERI 5: PENGENALAN DATABASE MYSQL DAN PROSES CRUD

if ($stmt->execute()) {
// echo "Data berhasil ditambahkan!";
header('Location:read.php');
} else {
echo "Gagal menambahkan data.";
}
}
?>

<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">

<div class="box">

<nav class="breadcrumb is-left" aria-label="breadcrumbs">


<ul>
<li><a href="#">Read</a></li>
<li class="is-active"><a href="#" aria-current="page">create</a></li>
</ul>
</nav>

<h1 class="title">Tambah Pelanggan</h1>

<form method="POST">
<div class="field">
<label class="label">Name</label>
<div class="control">
<input class="input" type="nama" placeholder="Masukan Nama" name="nama"
required>
</div>
</div>

<div class="field">
<label class="label">Name</label>
<div class="control">
<input class="input" type="email" placeholder="Masukan Email" name="email"
required>
</div>
</div>

<div class="field">
<label class="label">Name</label>
<div class="control">
<input class="input" type="number" placeholder="Masukan Usia" name="umur"
required>
</div>
</div>
<button class="button is-primary is-small" type="submit">Tambah</button>
</form>
</div>

Kode diatas disimpan : C:\xampp\htdocs\progint2\minggu5/create.php

Apabila tombol tambah diklik makah akan tampil form sebagai berikut :
5
Iyus Supriadi, S.T., M.T. – [email protected]
MATERI 5: PENGENALAN DATABASE MYSQL DAN PROSES CRUD

Koding delete.php
<?php
require 'db.php';

if (isset($_GET["id"])) {
$id = $_GET["id"];

$sql = "DELETE FROM pelanggan WHERE id = :id";


$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $id);

if ($stmt->execute()) {
// echo "Data berhasil dihapus!";
header('Location:read.php');
} else {
echo "Gagal menghapus data.";
}
}
?>
<a href="read.php">Kembali ke daftar</a>
6
Iyus Supriadi, S.T., M.T. – [email protected]
MATERI 5: PENGENALAN DATABASE MYSQL DAN PROSES CRUD

Kode diatas disimpan : C:\xampp\htdocs\progint2\minggu5/delete.php


Apabila tombol delete diklik maka akan menghapus data dan mengurati record

Koding update.php
<?php
require 'db.php';

if (isset($_GET["id"])) {
$id = $_GET["id"];
$stmt = $conn->prepare("SELECT * FROM pelanggan WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$pelanggan = $stmt->fetch(PDO::FETCH_ASSOC);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = $_POST["id"];
$nama = $_POST["nama"];
$email = $_POST["email"];
$umur = $_POST["umur"];

$sql = "UPDATE pelanggan SET nama = :nama, email = :email, umur = :umur WHERE id =
:id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':nama', $nama);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':umur', $umur);
$stmt->bindParam(':id', $id);

if ($stmt->execute()) {
// echo "Data berhasil diperbarui!";
header('Location:read.php');
} else {
echo "Gagal memperbarui data.";
}
}
7
Iyus Supriadi, S.T., M.T. – [email protected]
MATERI 5: PENGENALAN DATABASE MYSQL DAN PROSES CRUD

?>

<form method="POST">
<input type="hidden" name="id" value="<?= $pelanggan["id"] ?>">
<label>Nama:</label>
<input type="text" name="nama" value="<?= $pelanggan["nama"] ?>" required><br>
<label>Email:</label>
<input type="email" name="email" value="<?= $pelanggan["email"] ?>" required><br>
<label>Umur:</label>
<input type="number" name="umur" value="<?= $pelanggan["umur"] ?>" required><br>
<button type="submit">Update</button>
</form>

8
Iyus Supriadi, S.T., M.T. – [email protected]

You might also like