<?php
/* โปรแกรมโดย พิศิษฐ์ บวรเลิศสุธี ต้นฉบับจาก https://devbanban.com/?p=4146
นำโค้ดมาใช้เพื่อการศึกษา เรื่องโค้ดแบบ crud และการรวมฟังก์ชันแบบ sequential structure
สร้าง database และ table และ insert มี แนะนำ 2 วิธี
1. mysql -u root -p < devbanban.sql
2. phpmyadmin
---
CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `test`;
CREATE TABLE `tbl_member` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`surname` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tbl_member` (`id`, `name`, `surname`) VALUES
(1, 'นายจักรพัฒน์', 'ไชยแก้ว'),
(2, 'นายวรายุทธ', 'ขันก๋า'),
(3, 'นายสุริยา', 'พุฒดวง'),
(4, 'นางสาวนิชาภา', 'กะจันทร์'),
(5, 'นายสัมพันธ์', 'สุริยา'),
(6, 'นางสาวกวินทรา', 'ยะป๊อก'),
(7, 'นายพิศิษฐ์', 'บวรเลิศสุธี'),
(8, 'นายบุรินทร์', 'รุจจนพันธุ์'),
(9, 'นายวิเชพ', 'ใจบุญ');
(10, 'นางสาวเกศริน', 'อินเพลา');
(11, 'นางศศิวิมล', 'แรงสิงห์');
ALTER TABLE `tbl_member` ADD PRIMARY KEY (`id`);
ALTER TABLE `tbl_member` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
*/
$index = "crud_devbanban.php";
/* connect.php */
$servername = "localhost";
$username = "root";
$password = ""; //ถ้าไม่ได้ตั้งรหัสผ่านให้ลบ yourpassword ออก
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
/* del.php */
if(isset($_GET['id']) && isset($_GET['act']) && $_GET['act'] == "del"){
//require_once 'connect.php';
$id = $_GET['id'];
$stmt = $conn->prepare('DELETE FROM tbl_member WHERE id=:id');
$stmt->bindParam(':id', $id , PDO::PARAM_INT);
$stmt->execute();
echo '<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">';
// sweet alert
if($stmt->rowCount() > 0){
echo '<script>
setTimeout(function() {
swal({
title: "ลบข้อมูลสำเร็จ", type: "success"
}, function() { window.location = "' . $index .'"; });
}, 1000);
</script>';
}else{
echo '<script>
setTimeout(function() {
swal({
title: "เกิดข้อผิดพลาด", type: "error"
}, function() { window.location = "' . $index .'"; });
}, 1000);
</script>';
}
$conn = null;
die('<meta http-equiv="refresh" content="0; url='. $index .'">');
}
/* formedit.php */
if(isset($_GET['id']) && isset($_GET['act']) && $_GET['act'] == "edit"){
echo '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<title>Basic CRUD PHP PDO by devbanban.com 2021</title>
</head><body>';
if(isset($_GET['id'])){
//require_once 'connect.php';
$stmt = $conn->prepare("SELECT* FROM tbl_member WHERE id=?");
$stmt->execute([$_GET['id']]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() < 1){
header('Location: '. $index);
exit();
}
}
die('<div class="container">
<div class="row">
<div class="col-md-4"><br/>
<h4>ฟอร์มแก้ไขข้อมูล</h4>
<form action="'.$index.'" method="post">
<div class="mb-1">
<label for="name" class="col-sm-2 col-form-label"> ชื่อ : </label>
<div class="col-sm-10">
<input type="text" name="name" class="form-control" required value="'. $row['name'].'" minlength="3">
</div>
</div>
<div class="mb-1">
<label for="name" class="col-sm-2 col-form-label"> นามสกุล : </label>
<div class="col-sm-10">
<input type="text" name="surname" class="form-control" required value="'. $row['surname'].'" minlength="3">
</div>
</div>
<input type="hidden" name="id" value="'. $row['id'].'">
<button type="submit" class="btn btn-primary">แก้ไขข้อมูล</button>
</form>
</div></div></div>
</body></html>');
}
/* formedit_db.php */
if(isset($_POST['name']) && isset($_POST['surname']) && isset($_POST['id'])) {
// require_once 'connect.php';
$id = $_POST['id'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$stmt = $conn->prepare("UPDATE tbl_member SET name=:name, surname=:surname WHERE id=:id");
$stmt->bindParam(':id', $id , PDO::PARAM_INT);
$stmt->bindParam(':name', $name , PDO::PARAM_STR);
$stmt->bindParam(':surname', $surname , PDO::PARAM_STR);
$stmt->execute();
// sweet alert
echo '<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">';
if($stmt->rowCount() > 0){
echo '<script>
setTimeout(function() {
swal({
title: "แก้ไขข้อมูลสำเร็จ", type: "success"
}, function() { window.location = "'. $index .'"; });
}, 1000);
</script>';
}else{
echo '<script>
setTimeout(function() {
swal({
title: "เกิดข้อผิดพลาด", type: "error"
}, function() { window.location = "'. $index .'"; });
}, 1000);
</script>';
}
$conn = null;
die('<meta http-equiv="refresh" content="0; url='. $index .'">');
}
/* formAdd.php */
if(isset($_GET['act']) && $_GET['act'] == "add"){
die ('<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<title>Basic CRUD PHP PDO by devbanban.com 2021</title>
</head><body>
<div class="container">
<div class="row">
<div class="col-md-4"> <br/>
<h4>ฟอร์มเพิ่มข้อมูล</h4>
<form action="?" method="post">
<div class="mb-1">
<label for="name" class="col-sm-2 col-form-label"> ชื่อ : </label>
<div class="col-sm-10">
<input type="text" name="name" class="form-control" required minlength="3" placeholder="ชื่อ">
</div>
</div>
<div class="mb-1">
<label for="name" class="col-sm-2 col-form-label"> นามสกุล : </label>
<div class="col-sm-10">
<input type="text" name="surname" class="form-control" required minlength="3" placeholder="นามสกุล">
</div>
</div>
<button type="submit" class="btn btn-primary">เพิ่มข้อมูล</button>
</form>
</div>
</div>
</div>
</body></html>');
}
/* formAdd_db.php */
if(isset($_POST['name']) && isset($_POST['surname'])){
// require_once 'connect.php';
$name = $_POST['name'];
$surname = $_POST['surname'];
$stmt = $conn->prepare("INSERT INTO tbl_member (name, surname) VALUES (:name, :surname)");
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':surname', $surname , PDO::PARAM_STR);
$result = $stmt->execute();
// sweet alert
echo '<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">';
if($result){
echo '<script>
setTimeout(function() {
swal({
title: "เพิ่มข้อมูลสำเร็จ", type: "success"
}, function() { window.location = "index.php"; });
}, 1000);
</script>';
}else{
echo '<script>
setTimeout(function() {
swal({
title: "เกิดข้อผิดพลาด", type: "error"
}, function() { window.location = "index.php"; });
}, 1000);
</script>';
}
$conn = null; //close connect db
die('<meta http-equiv="refresh" content="0; url='. $index .'">');
} //isset
/* index.php */
?><!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<title>Basic CRUD PHP PDO by devbanban.com 2021</title></head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12"> <br/>
<h3>รายการสมาชิก <a href="?act=add" class="btn btn-info">+เพิ่มข้อมูล</a> </h3>
<table class="table table-striped table-hover table-responsive table-bordered">
<thead>
<tr>
<th width="5%">ลำดับ</th>
<th width="40%">ชื่อ</th>
<th width="45%">นามสกุล</th>
<th width="5%">แก้ไข</th>
<th width="5%">ลบ</th>
</tr>
</thead>
<tbody>
<?php
// require_once 'connect.php';
$stmt = $conn->prepare("SELECT* FROM tbl_member");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $k) { ?>
<tr>
<td><?= $k['id'];?></td>
<td><?= $k['name'];?></td>
<td><?= $k['surname'];?></td>
<td><a href="?act=edit&id=<?= $k['id'];?>" class="btn btn-warning btn-sm">แก้ไข</a></td>
<td><a href="?act=del&id=<?= $k['id'];?>" class="btn btn-danger btn-sm"
onclick="return confirm('ยืนยันการลบข้อมูล !!');">ลบ</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
<center>Basic CRUD PHP PDO by devbanban.com 2021</center>
</body></html>
จำนวน : 265 บรรทัด