Workshop ShoppingCart Product Stock Update with PHP8 PDO
- ระบบอัปเดตสต๊อกสินค้าเป็นส่วนสำคัญของระบบจัดการสินค้า โดยช่วยให้ผู้ดูแลระบบสามารถตรวจสอบจำนวนสินค้าและปรับปรุงสต๊อกสินค้าได้อย่างสะดวก โดยขั้นตอนการทำงานของระบบมีดังนี้
-
การพัฒนาโครงสร้างหน้าจัดการสต๊อกสินค้า
ในส่วนนี้เป็นการออกแบบหน้าเว็บสำหรับจัดการและแสดงรายการสต๊อกสินค้า โดยใช้โครงสร้าง Template เพื่อให้สามารถจัดการโค้ดได้ง่าย และสามารถนำกลับมาใช้ซ้ำได้
โดยหน้าเว็บนี้ถูกพัฒนาด้วย PHP ร่วมกับ AdminLTE Framework
-
|
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 |
<!DOCTYPE html> <html lang="en"> <?php $menu = "stock_product";?> <!-- เรียกใช้ไฟล์ haed --> <?php include'head.php'; ?> <body class="hold-transition sidebar-mini layout-fixed"> <div class="wrapper"> <!-- Navbar --> <?php include'nav.php'; ?> <!-- /.navbar --> <!-- Main Sidebar Container --> <?php include'menu.php'; ?> <!-- Content Wrapper. Contains page content --> <div class="content-wrapper"> <!-- Content Header (Page header) --> <div class="content-header"> <div class="container-fluid"> <?php include('product_stock_list.php'); ?> </div><!-- /.container-fluid --> </div> </div> <!-- /.content-wrapper --> <?php include'footer.php'; ?> <!-- Control Sidebar --> <aside class="control-sidebar control-sidebar-dark"> <!-- Control sidebar content goes here --> </aside> <!-- /.control-sidebar --> </div> <!-- ./wrapper --> <?php include'script.php'; ?> </body> </html> |
-
-
การดึงข้อมูลสินค้าจากฐานข้อมูล
- ระบบจะทำการเชื่อมต่อฐานข้อมูล และดึงข้อมูลสินค้าจากตาราง
tbl_productรวมถึงรูปภาพสินค้าจากตารางtbl_img_detailเพื่อนำมาแสดงในรูปแบบตาราง
- ระบบจะทำการเชื่อมต่อฐานข้อมูล และดึงข้อมูลสินค้าจากตาราง
-
การแสดงข้อมูลสินค้าในรูปแบบตาราง
- ข้อมูลสินค้าจะถูกนำมาแสดงในตาราง โดยประกอบด้วย
-
ลำดับสินค้า
-
รูปสินค้า
-
ชื่อสินค้า
-
สถานะสินค้า
-
จำนวนสินค้าในคลัง
-
ช่องกรอกจำนวนสต๊อกที่ต้องการปรับ
-
ปุ่มสำหรับปรับสต๊อก
-
- ข้อมูลสินค้าจะถูกนำมาแสดงในตาราง โดยประกอบด้วย
-
- ตัวอย่าง product_stock_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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
<?php //คิวรี่ข้อมูลมาแสดงในตาราง include '../condb.php'; $stmtProduct = $conn->prepare(" SELECT p.*, MIN(i.img) AS img FROM tbl_product as p INNER JOIN tbl_img_detail as i ON p.p_id = i.p_id GROUP BY p.p_id ORDER BY p.p_id ASC #เรียงลำดับข้อมูลจากน้อยไปมาก "); $stmtProduct->execute(); $resultProduct = $stmtProduct->fetchAll(); ?> <style type="text/css" media="screen"> .light-gray { color: #616A6B; /* เลือกสีที่ต้องการ */ } </style> <table id="example1" class="table table-striped dataTable"> <thead> <tr role="row" class="info"> <th tabindex="0" rowspan="1" colspan="1" style="width: 1%;">No.</th> <th tabindex="0" rowspan="1" colspan="1" style="width: 5%;">รูป</th> <th tabindex="0" rowspan="1" colspan="1" style="width: 30%;">สินค้า</th> <th tabindex="0" rowspan="1" colspan="1" style="width: 20%;">สถานะ</th> <th style="width: 10%; text-align: center">จำนวน</th> <th tabindex="0" rowspan="1" colspan="1" style="width: 10%;">สต๊อกสินค้า</th> <th tabindex="0" rowspan="1" colspan="1" style="width: 10%;">ปรับสต๊อก</th> </tr> </thead> <tbody> <?php $runNumber = 1; foreach ($resultProduct as $row_prod) { ?> <tr> <td align="center"> <?php echo $runNumber++; ?> </td> <td align="center"> <img src="../p_gallery/<?php echo $row_prod['img']; ?>" width="60"> </td> <td> <?php echo $row_prod['p_name']; ?> </td> <td> <?php if ($row_prod['p_qty'] < 10) { echo '<span class="badge badge-danger" style="font-size: 14px;">สินค้าใกล้หมดแล้ว</span>'; } else { echo 'สินค้าสามารถสั่งซื้อได้'; } ?> </td> <td align="center"> <?php echo $row_prod['p_qty']; ?> </td> <td> <input type="number" name="p_qty_<?php echo $row_prod['p_id']; ?>" class="form-control"> <input type="hidden" name="p_id" value="<?php echo $row_prod['p_id']; ?>"> </td> <td> <a href="#" class="btn btn-outline-dark btn-sm" onclick="confirmStock(<?php echo $row_prod['p_id']; ?>)"> <i class="fas fa-cart-plus"></i> +ปรับสต๊อก </a> </td> <?php } ?> </tr> </tbody> </table> <script> function confirmStock(p_id) { Swal.fire({ title: 'ยืนยันการปรับสต๊อก', text: 'คุณต้องการที่จะปรับสต๊อกสินค้าใช่หรือไม่?', icon: 'question', showCancelButton: true, confirmButtonText: 'ใช่', cancelButtonText: 'ไม่', reverseButtons: true }).then((result) => { if (result.isConfirmed) { adjustStock(p_id); } }); } function adjustStock(p_id) { var qtyInput = document.querySelector('input[name="p_qty_' + p_id + '"]'); var qty = qtyInput.value.trim(); // Check if quantity is empty if (qty === '') { Swal.fire('ผิดพลาด!', 'กรุณากรอกจำนวนสินค้า', 'error'); return; } // AJAX call to update stock var xhr = new XMLHttpRequest(); xhr.open("POST", "update_stock.php", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status === 200) { // Handle success Swal.fire('สำเร็จ!', xhr.responseText, 'success'); // Reload the page after a short delay setTimeout(function() { window.location.reload(); }, 1000); } else { // Handle error Swal.fire('ผิดพลาด!', 'มีบางอย่างผิดพลาดในการปรับสต็อก', 'error'); } } }; xhr.send("p_id=" + p_id + "&p_qty=" + qty); } </script> |
-
อัปเดตสต๊อกสินค้า (update_stock.php)
ไฟล์
update_stock.phpทำหน้าที่รับข้อมูลจากผู้ใช้งาน และดำเนินการอัปเดตจำนวนสินค้าในฐานข้อมูล โดยใช้ภาษา PHP ร่วมกับ PDO เพื่อเพิ่มความปลอดภัยในการจัดการข้อมูล
|
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 |
<?php include '../condb.php'; if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['p_id']) && isset($_POST['p_qty'])) { $p_id = filter_var($_POST['p_id'], FILTER_SANITIZE_NUMBER_INT); $p_qty = filter_var($_POST['p_qty'], FILTER_SANITIZE_NUMBER_INT); // ดึงจำนวนสต๊อกปัจจุบัน $stmt = $conn->prepare("SELECT p_qty FROM tbl_product WHERE p_id = :p_id"); $stmt->bindParam(':p_id', $p_id); $stmt->execute(); $current_qty = $stmt->fetchColumn(); if ($current_qty !== false) { // คำนวณจำนวนสต๊อกใหม่ $new_qty = $current_qty + $p_qty; // อัปเดตสต๊อกในฐานข้อมูล $stmt = $conn->prepare("UPDATE tbl_product SET p_qty = :p_qty WHERE p_id = :p_id"); $stmt->bindParam(':p_qty', $new_qty); $stmt->bindParam(':p_id', $p_id); if ($stmt->execute()) { echo "ปรับสต๊อกสำเร็จ"; } else { echo "เกิดข้อผิดพลาดในการอัปเดตสต๊อก"; } } else { echo "ไม่พบสินค้า"; } } else { echo "คำขอไม่ถูกต้อง"; } ?> |
-
ขั้นตอนการทำงานของระบบ
-
รับข้อมูลจากผู้ใช้งานผ่าน AJAX
-
ตรวจสอบความถูกต้องของข้อมูล
-
ดึงข้อมูลสต๊อกเดิม
-
คำนวณจำนวนสต๊อกใหม่
-
อัปเดตข้อมูลลงฐานข้อมูล
-
ส่งผลลัพธ์กลับไปแสดงบนหน้าเว็บ
-

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