สวัสดีครับ ในบทความนี้จะแจกตัวอย่างโค้ดระบบเพิ่ม ลบ แก้ไข และแสดงข้อมูลแบบง่ายๆ โดยเขียนในรูปแบบ PHP PDO รองรับการทำงาน PHP7 และ PHP8 สำหรับเอาไปศึกษาและพัฒนาต่อยอดครับ
ขายโปรแกรม ลดสูงสุด 70%
ขายระบบสารสนเทศออนไลน์ พร้อมใช้ ราคาเบาๆ ได้โค้ดทั้งหมด นำไปต่อยอดได้
สินค้าทั้งหมด คลิก https://devbanban.com/?p=4425
ซื้อแล้วปรึกษาได้เรื่อยๆ ครับ
สนใจ inbox มาที่แฟนเพจ หรือ ☎️ 0948616709
รวมคลิปสอนปรับพื้นฐาน ภาษา php ฟรี 100 คลิป ++ https://devbanban.com/?p=3940
โค้ดประกอบด้วย
- PHP PDO
- MySQL
- HTML
- Bootstrap5
- Sweet Alert
มาเริ่มทำ workshop กันครับ *คอมเม้นอยู่ในโค้ด
0. รวม workshop PHP PDO ทั้งหมด คลิก
1.ฐานข้อมูลที่ให้จะชื่อ workshop_pdo ไปที่เมนู sql คัดลอกโค้ดไปวางและ go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
CREATE DATABASE IF NOT EXISTS `workshop_pdo` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE `workshop_pdo`; 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, 'Tony', 'Stark'), (2, 'Thor', 'Odinson'), (3, 'Dr. Bruce', 'Banner'), (4, 'Wanda', 'Maximoff'), (5, 'Natasha', 'Alianovna Romanoff'), (6, 'Peter', 'Benjamin Parker'), (7, 'Clinton', 'Francis Barton'), (8, 'Yondu', 'Udonta'), (9, 'Steven', 'Rogers'); ALTER TABLE `tbl_member` ADD PRIMARY KEY (`id`); ALTER TABLE `tbl_member` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10; |
2.ไฟล์เชื่อมต่อฐานข้อมูล ชื่อไฟล์ connect.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $servername = "localhost"; $username = "root"; $password = "yourpassword"; //ถ้าไม่ได้ตั้งรหัสผ่านให้ลบ yourpassword ออก try { $conn = new PDO("mysql:host=$servername;dbname=workshop_pdo;charset=utf8", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> |
3.ไฟล์แสดงหน้าแรกของระบบ index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<!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="formAdd.php" 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="formEdit.php?id=<?= $k['id'];?>" class="btn btn-warning btn-sm">แก้ไข</a></td> <td><a href="del.php?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> |
4.ไฟล์ฟอร์มเพิ่มข้อมูล formAdd.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<!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="formAdd_db.php" 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> |
5.ไฟล์รับค่าจากฟอร์มและเพิ่มเข้าฐานข้อมูล formAdd_db.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?php //ถ้ามีค่าส่งมาจากฟอร์ม if(isset($_POST['name']) && isset($_POST['surname'])){ //ไฟล์เชื่อมต่อฐานข้อมูล require_once 'connect.php'; //ประกาศตัวแปรรับค่าจากฟอร์ม $name = $_POST['name']; $surname = $_POST['surname']; //sql insert $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 } //isset ?> |
6.ฟอร์มแก้ไขข้อมูล formedit.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<!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> <?php 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); //ถ้าคิวรี่ผิดพลาดให้กลับไปหน้า index if($stmt->rowCount() < 1){ header('Location: index.php'); exit(); } }//isset ?> <div class="container"> <div class="row"> <div class="col-md-4"> <br> <h4>ฟอร์มแก้ไขข้อมูล</h4> <form action="formedit_db.php" 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> |
7.ไฟล์รับค่าจากฟอร์มและส่งไปแก้ไขข้อมูลในตาราง formedit_db.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php //ถ้ามีค่าส่งมาจากฟอร์ม if(isset($_POST['name']) && isset($_POST['surname']) && isset($_POST['id'])) { //ไฟล์เชื่อมต่อฐานข้อมูล require_once 'connect.php'; //ประกาศตัวแปรรับค่าจากฟอร์ม $id = $_POST['id']; $name = $_POST['name']; $surname = $_POST['surname']; //sql update $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.php"; //หน้าที่ต้องการให้กระโดดไป }); }, 1000); </script>'; }else{ echo '<script> setTimeout(function() { swal({ title: "เกิดข้อผิดพลาด", type: "error" }, function() { window.location = "index.php"; //หน้าที่ต้องการให้กระโดดไป }); }, 1000); </script>'; } $conn = null; //close connect db } //isset ?> |
8.ไฟล์ลบข้อมูล del.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php if(isset($_GET['id'])){ require_once 'connect.php'; //ประกาศตัวแปรรับค่าจาก param method get $id = $_GET['id']; $stmt = $conn->prepare('DELETE FROM tbl_member WHERE id=:id'); $stmt->bindParam(':id', $id , PDO::PARAM_INT); $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() ==1){ 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; } //isset ?> |
ผลการทำงาน
ลองเอาตัวอย่างโค้ดไปรันและต่อยอดนะครับ ^^
แหล่งศึกษาเพิ่มเติม : https://websitebeaver.com/php-pdo-vs-mysqli
List of PDOStatement::bindParam data_type parameters : https://www.php.net/manual/en/pdo.constants.php
ขายโปรแกรม ลดสูงสุด 70%
ขายระบบสารสนเทศออนไลน์ พร้อมใช้ ราคาเบาๆ ได้โค้ดทั้งหมด นำไปต่อยอดได้
สินค้าทั้งหมด คลิก https://devbanban.com/?p=4425
ซื้อแล้วปรึกษาได้เรื่อยๆ ครับ
สนใจ inbox มาที่แฟนเพจ หรือ ☎️ 0948616709
ร่วมสนับสนุน ค่ากาแฟ ค่าโฮส devbanban.com ได้ที่
ธนาคารกรุงไทย สาขาเดอะมอลล์ท่าพระ
ชื่อบัญชี นายพิศิษฐ์ บวรเลิศสุธี เลขที่ 878-0-17747-6
————————————————————————————
ธนาคารไทยพาณิชย์ สาขามหาวิทยาลัยราชภัฏธนบุรี
ชื่อบัญชี นายพิศิษฐ์ บวรเลิศสุธี เลขที่ 406-359094-1
fanpage : https://www.facebook.com/sornwebsites/