Workshop สอนทำระบบเบิกจ่ายวัสดุอุปกรณ์ PHP8 PDO MySQL AdminLTE
รวมคลิปสอนทำระบบ POS Laravel + AdminLTE
- ตัวอย่างโค้ดแสดงข้อมูลสมาชิก department_list.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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<?php // เชื่อมต่อฐานข้อมูล require_once '../condb.php'; // เตรียมคำสั่ง SQL $sql = "SELECT d_id, d_name FROM departments"; $stmt = $con->prepare($sql); $stmt->execute(); // รันคำสั่ง $departments = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <table id="example1" class="table table-striped dataTable"> <thead> <tr> <th style="width: 1%;">ลำดับ</th> <th style="width: 80%;">ชื่อแผนก</th> <th style="width: 5%;">จัดการ</th> </tr> </thead> <tbody> <?php $i = 1; foreach ($departments as $dep): ?> <tr> <td><?= $i++ ?></td> <td><?= htmlspecialchars($dep['d_name']) ?></td> <td> <a href="#" class="btn btn-warning btn-sm editBtn" data-id="<?= $dep['d_id'] ?>"> <i class="fas fa-pencil-alt"></i> </a> <a href="#" class="btn btn-danger btn-sm deleteBtn" data-id="<?= $dep['d_id'] ?>" title="ลบ"> <i class="fas fa-trash"></i> </a> </td> </tr> <?php endforeach; ?> </tbody> </table> <script> $(".editBtn").click(function(){ var d_id = $(this).data("id"); $.ajax({ url: "dep_get.php", // เขียนไฟล์ดึงข้อมูล department type: "GET", data: { d_id: d_id }, dataType: "json", success: function(res){ $("#edit_d_id").val(res.d_id); $("#edit_d_name").val(res.d_name); $("#editDepartmentModal").modal("show"); } }); }); $(".deleteBtn").click(function(e){ e.preventDefault(); var d_id = $(this).data("id"); if(confirm("ต้องการลบข้อมูลใช่หรือไม่?")){ $.ajax({ url: "dep_delete.php", // เขียนไฟล์ลบ department type: "POST", data: { d_id: d_id }, dataType: "json", success: function(res){ if(res.status === "success"){ toastr.success(res.message); setTimeout(() => { location.reload(); }, 1000); } else { toastr.error(res.message); } }, error: function(xhr){ toastr.error("เกิดข้อผิดพลาด: " + xhr.responseText); } }); } }); </script> |
- สร้างปุ่มสำหรับเพิ่มข้อมูล department.php
|
1 2 3 4 5 6 7 8 9 10 11 |
<div class="container-fluid"> <div class="d-flex justify-content-between align-items-center mb-3"> <a href="department.php?act=add" type="button" class="btn btn-primary btn-rounded btn-sm" data-toggle="modal" data-target=".bd-example-modal-department"> <i class="fas fa-plus-circle"></i> เพิ่มข้อมูล </a> <?php include('department_add.php'); ?> </div> <?php include('department_edit.php'); include('department_list.php'); ?> </div> |
- สร้างฟอร์มสำหรับเพิ่มข้อมูลแผนก department_add.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 51 52 53 54 55 56 57 |
<!-- Modal เพิ่มแผนก --> <div class="modal fade bd-example-modal-department" tabindex="-1" role="dialog" aria-labelledby="modalDepartmentLabel" aria-hidden="true"> <div class="modal-dialog modal-md"> <div class="modal-content"> <form id="departmentForm" action="department_add_db.php" method="post"> <div class="modal-header"> <h5 class="modal-title" id="modalDepartmentLabel"><b>เพิ่มแผนกใหม่</b></h5> <button type="button" class="close" data-dismiss="modal" aria-label="ปิด"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <div class="form-group"> <label for="d_name">ชื่อแผนก</label> <input type="text" name="d_name" class="form-control" placeholder="ป้อนชื่อแผนก" required> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-success">บันทึกข้อมูล</button> <button type="button" class="btn btn-danger" data-dismiss="modal">ยกเลิก</button> </div> </form> </div> </div> </div> <script> $("#departmentForm").on("submit", function(e) { e.preventDefault(); $.ajax({ url: "department_add_db.php", type: "POST", data: $(this).serialize(), dataType: "json", success: function(response) { if (response.status === "success") { toastr.success(response.message); $("#departmentForm")[0].reset(); $(".bd-example-modal-department").modal("hide"); setTimeout(() => { window.location.href = "department.php"; }, 2000); } else { toastr.error(response.message); } }, error: function(xhr, status, error) { toastr.error("เกิดข้อผิดพลาด: " + error); } }); }); </script> |
- บันทึกข้อมูลลงฐาข้อมูล department_add_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 |
<?php header('Content-Type: application/json'); require_once '../condb.php'; // ไฟล์เชื่อมต่อฐานข้อมูล $d_name = trim($_POST['d_name']); // รับค่าจากฟอร์ม try { if (empty($d_name)) { echo json_encode([ "status" => "error", "message" => "กรุณากรอกชื่อแผนก!" ]); exit; } // ตรวจสอบชื่อแผนกซ้ำ $check = $con->prepare("SELECT d_id FROM departments WHERE d_name = :d_name LIMIT 1"); $check->execute([":d_name" => $d_name]); if ($check->rowCount() > 0) { echo json_encode([ "status" => "error", "message" => "มีชื่อแผนกนี้แล้ว!" ]); exit; } // เพิ่มข้อมูล $sql = "INSERT INTO departments (d_name) VALUES (:d_name)"; $stmt = $con->prepare($sql); $stmt->execute([":d_name" => $d_name]); echo json_encode([ "status" => "success", "message" => "เพิ่มข้อมูลแผนกเรียบร้อยแล้ว!" ]); } catch (Exception $e) { echo json_encode([ "status" => "error", "message" => "เกิดข้อผิดพลาด: " . $e->getMessage() ]); } |
- สร้างฟอร์มสำหรับแก้ไขข้อมูลแผนก department_edit.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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<div class="modal fade" id="editDepartmentModal" tabindex="-1" role="dialog"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <form id="editDepartmentForm"> <div class="modal-header"> <h5 class="modal-title">แก้ไขแผนก</h5> <button type="button" class="close" data-dismiss="modal">×</button> </div> <div class="modal-body"> <input type="hidden" name="d_id" id="edit_d_id"> <div class="form-group"> <label>ชื่อแผนก</label> <input type="text" name="d_name" id="edit_d_name" class="form-control" required> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-success">บันทึก</button> <button type="button" class="btn btn-danger" data-dismiss="modal">ยกเลิก</button> </div> </form> </div> </div> </div> <script> $(document).ready(function() { // ปุ่มแก้ไขแผนก $(".editBtn").click(function() { var d_id = $(this).data("id"); $.ajax({ url: "department_get.php", type: "GET", data: { d_id: d_id }, dataType: "json", success: function(res) { if(res.error){ toastr.error(res.error); } else { $("#edit_d_id").val(res.d_id); $("#edit_d_name").val(res.d_name); $("#editDepartmentModal").modal("show"); } } }); }); // Submit ฟอร์มแก้ไข $("#editDepartmentForm").submit(function(e) { e.preventDefault(); $.ajax({ url: "department_edit_db.php", type: "POST", data: $(this).serialize(), dataType: "json", success: function(res) { if (res.status === "success") { toastr.success(res.message); $("#editDepartmentModal").modal("hide"); setTimeout(() => { location.reload(); }, 1500); } else { toastr.error(res.message); } }, error: function(xhr) { toastr.error("เกิดข้อผิดพลาด: " + xhr.responseText); } }); }); }); </script> |
- ไฟล์สำหรับ get ข้อมูล departments มาแสดงในฟอร์มแก้ไข dep_get.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php header('Content-Type: application/json'); require_once '../condb.php'; $d_id = $_GET['d_id'] ?? 0; $stmt = $con->prepare("SELECT * FROM departments WHERE d_id = :d_id LIMIT 1"); $stmt->execute([':d_id' => $d_id]); $data = $stmt->fetch(PDO::FETCH_ASSOC); if ($data) { echo json_encode($data); } else { echo json_encode(["error" => "ไม่พบข้อมูล"]); } |
- แก้ไขข้อมูลลงฐานข้อมูล department_edit_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 |
<?php header('Content-Type: application/json'); require_once '../condb.php'; $d_id = $_POST['d_id']; $d_name = trim($_POST['d_name']); try { if (empty($d_name)) { echo json_encode([ "status" => "error", "message" => "กรุณากรอกชื่อแผนก!" ]); exit; } // ตรวจสอบชื่อแผนกซ้ำ (ยกเว้นตัวเอง) $check = $con->prepare("SELECT d_id FROM departments WHERE d_name = :d_name AND d_id != :d_id LIMIT 1"); $check->execute([":d_name" => $d_name, ":d_id" => $d_id]); if ($check->rowCount() > 0) { echo json_encode([ "status" => "error", "message" => "มีชื่อแผนกนี้แล้ว!" ]); exit; } $sql = "UPDATE departments SET d_name = :d_name WHERE d_id = :d_id"; $stmt = $con->prepare($sql); $stmt->execute([":d_name" => $d_name, ":d_id" => $d_id]); echo json_encode([ "status" => "success", "message" => "แก้ไขข้อมูลเรียบร้อยแล้ว!" ]); } catch (Exception $e) { echo json_encode([ "status" => "error", "message" => "เกิดข้อผิดพลาด: " . $e->getMessage() ]); } |
- ลบข้อมูล departmens ออกจากฐานข้อมูล dep_delete.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 header('Content-Type: application/json'); require_once '../condb.php'; // ไฟล์เชื่อมต่อฐานข้อมูล if (!isset($_POST['d_id'])) { echo json_encode([ "status" => "error", "message" => "ไม่พบรหัสแผนก" ]); exit; } $d_id = $_POST['d_id']; try { // ตรวจสอบว่ามีแผนกนี้หรือไม่ $check = $con->prepare("SELECT d_id FROM departments WHERE d_id = :d_id"); $check->execute([":d_id" => $d_id]); if ($check->rowCount() === 0) { echo json_encode([ "status" => "error", "message" => "ไม่พบข้อมูลแผนกนี้" ]); exit; } // ลบแผนก $stmt = $con->prepare("DELETE FROM departments WHERE d_id = :d_id"); $stmt->execute([":d_id" => $d_id]); echo json_encode([ "status" => "success", "message" => "ลบแผนกเรียบร้อยแล้ว!" ]); } catch (Exception $e) { echo json_encode([ "status" => "error", "message" => "เกิดข้อผิดพลาด: " . $e->getMessage() ]); } |
ระบบเต็ม ShoppingCart จากราคา 2999 เหลือ 2000 บาทถ้วน

สนใจจ้างพัฒนาระบบ เขียนระบบ
ทำเว็บไซต์ รับสอนเขียนโปรแกรม
inbox มาที่เพจ หรือติดต่อ 098-373-8651
ช่องทางการชำระเงิน
เงินสด หรือ e-banking
ธนาคารกสิกรไทย
เลขบัญชี : 0951168564
ชื่อบัญชี : นายวัยวุฒิ ชุมเมืองปัก
