สร้างตัวอย่าง ระบบ
หน้าแสดงรายชื่อสมาชิก,
หน้าเพิ่มสมาชิก,
หน้าแก้ไข และลบข้อมูล ด้วย php mysql
1. สร้างฐานข้อมูลขึ้นมา แล้ว นำโค้ดนี้ไปสร้างในเมนู sql
CREATE DATABASE member_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE member_db;
CREATE TABLE members (
id INT AUTO_INCREMENT PRIMARY KEY,
fullname VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(20) NOT NULL
);
2.สร้างหน้าติดต่อฐานข้อมูล (db.php)
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "member_db";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("เชื่อมต่อฐานข้อมูลล้มเหลว: " . $conn->connect_error);
}
$conn->set_charset("utf8mb4");
?>
3.สร้างหน้าแรกเพื่อแสดงรายชื่อสมาชิก (index.php)
<?php
include 'db.php';
// ดึงข้อมูลสมาชิก
$result = $conn->query("SELECT * FROM members ORDER BY id DESC");
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>รายชื่อสมาชิก</title>
</head>
<body>
<h2>รายชื่อสมาชิก</h2>
<a href="add.php">+ เพิ่มสมาชิก</a>
<table border="1" cellpadding="8" cellspacing="0">
<tr>
<th>ลำดับ</th>
<th>ชื่อ-นามสกุล</th>
<th>Email</th>
<th>เบอร์โทร</th>
<th>จัดการ</th>
</tr>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row['id']; ?></td>
<td><?= htmlspecialchars($row['fullname']); ?></td>
<td><?= htmlspecialchars($row['email']); ?></td>
<td><?= htmlspecialchars($row['phone']); ?></td>
<td>
<a href="edit.php?id=<?= $row['id']; ?>">แก้ไข</a> |
<a href="delete.php?id=<?= $row['id']; ?>" onclick="return confirm('คุณแน่ใจหรือไม่ที่จะลบ?');">ลบ</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
4. สร้างหน้าเพิ่มข้อมูล (add.php)
<?php
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$stmt = $conn->prepare("INSERT INTO members (fullname, email, phone) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $fullname, $email, $phone);
$stmt->execute();
header("Location: index.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>เพิ่มสมาชิก</title>
</head>
<body>
<h2>เพิ่มสมาชิก</h2>
<form method="post">
<label>ชื่อ-นามสกุล: <input type="text" name="fullname" required></label><br><br>
<label>Email: <input type="email" name="email" required></label><br><br>
<label>เบอร์โทร: <input type="text" name="phone" required></label><br><br>
<button type="submit">บันทึก</button>
<a href="index.php">ยกเลิก</a>
</form>
</body>
</html>
5. สร้างหน้าแก้ไขข้อมูลสมาชิก (edit.php)
<?php
include 'db.php';
$id = $_GET['id'] ?? 0;
// ดึงข้อมูลสมาชิก
$stmt = $conn->prepare("SELECT * FROM members WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$member = $result->fetch_assoc();
if (!$member) {
die("ไม่พบข้อมูลสมาชิก");
}
// เมื่อกดบันทึก
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$stmt = $conn->prepare("UPDATE members SET fullname=?, email=?, phone=? WHERE id=?");
$stmt->bind_param("sssi", $fullname, $email, $phone, $id);
$stmt->execute();
header("Location: index.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>แก้ไขสมาชิก</title>
</head>
<body>
<h2>แก้ไขสมาชิก</h2>
<form method="post">
<label>ชื่อ-นามสกุล: <input type="text" name="fullname" value="<?= htmlspecialchars($member['fullname']); ?>" required></label><br><br>
<label>Email: <input type="email" name="email" value="<?= htmlspecialchars($member['email']); ?>" required></label><br><br>
<label>เบอร์โทร: <input type="text" name="phone" value="<?= htmlspecialchars($member['phone']); ?>" required></label><br><br>
<button type="submit">บันทึก</button>
<a href="index.php">ยกเลิก</a>
</form>
</body>
</html>
6. สร้างหน้าลบข้อมูล (delete.php)
<?php
include 'db.php';
$id = $_GET['id'] ?? 0;
$stmt = $conn->prepare("DELETE FROM members WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
header("Location: index.php");
exit;
?>