แจกตัวอย่างโปรแกรมร้านขายของชำเพื่อเอาไปเป็นแนวทางและต่อยอด (gemini) รองรับการทำงานบน PHP v.7 – 8 . ฐานข้อมูลใช้ MySQL
แจกให้ศึกษาและพัฒนาต่อยอดเท่านั้น ห้ามนำไปใช้งานจริง
โปรโมชั่นพิเศษ ลดสูงสุด 80%!
ขาย Source Code พร้อมใช้งาน นำไปต่อยอดได้ทันที
ดูสินค้าทั้งหมด ได้ที่ : https://devbanban.com/?p=4425
ซื้อแล้วปรึกษาได้ตลอด
สนใจติดต่อ inbox ที่แฟนเพจ
☎️ โทร: 094-861-6709
Line OA : https://lin.ee/v8VwivgC
- รวมคลิปสอน PHP PDO MySQL (ฟรีคอร์ส) เข้าเรียน คลิก
- รวมคลิปสอน Laravel v.12 (ฟรีคอร์ส) เข้าเรียน คลิก
ภาพการทำงาน

ปล. Gen code จาก gemini ทำเล่นขำๆ เพื่อเป็นไอเดียครับ
0. database
|
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 |
CREATE DATABASE IF NOT EXISTS grocery_store; USE grocery_store; -- ตารางสินค้า CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, barcode VARCHAR(50) UNIQUE NOT NULL, name VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL, stock INT NOT NULL ); -- ตารางบันทึกการขาย CREATE TABLE sales ( id INT AUTO_INCREMENT PRIMARY KEY, product_id INT, quantity INT, total_price DECIMAL(10, 2), sale_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (product_id) REFERENCES products(id) ); -- ข้อมูลตัวอย่างสำหรับทดสอบสแกน INSERT INTO products (barcode, name, price, stock) VALUES ('111', 'นมกล่อง', 12.00, 50), ('222', 'ขนมปัง', 20.00, 30), ('333', 'น้ำดื่ม', 10.00, 100); |
1.db.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $host = 'localhost'; $db = 'grocery_store'; $user = 'root'; $pass = ''; $charset = 'utf8mb4'; try { $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $pdo = new PDO($dsn, $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]); } catch (PDOException $e) { die("Connection failed: " . $e->getMessage()); } ?> |
2. 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 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 |
<!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <title>ระบบขายหน้าร้าน - POS</title> <style> * { box-sizing: border-box; } body { font-family: 'Tahoma', sans-serif; background: #f0f2f5; margin: 0; padding: 20px; } .wrapper { max-width: 1000px; margin: auto; display: flex; gap: 20px; } /* ฝั่งซ้าย: ช่องยิงบาร์โค้ด */ .scan-box { flex: 1; background: #fff; padding: 30px; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; } #barcode { width: 100%; padding: 15px; font-size: 24px; border: 2px solid #ddd; border-radius: 8px; margin-top: 10px; outline: none; } #barcode:focus { border-color: #007bff; } /* ฝั่งขวา: ตะกร้าสินค้า */ .cart-box { flex: 1.5; background: #fff; padding: 20px; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); } table { width: 100%; border-collapse: collapse; margin-top: 15px; } th, td { padding: 12px; border-bottom: 1px solid #eee; text-align: left; } th { background: #f8f9fa; } .total-section { font-size: 32px; font-weight: bold; color: #d9534f; text-align: right; margin-top: 20px; } .status-msg { margin-top: 10px; font-weight: bold; } </style> </head> <body> <h2 style="text-align:center; color: #333;">🛒 Grocery Barcode POS</h2> <div class="wrapper"> <div class="scan-box"> <h3>สแกนบาร์โค้ด</h3> <input type="text" id="barcode" placeholder="รอสแกน..." autofocus> <div id="msg" class="status-msg"></div> <div style="margin-top: 40px; color: #888;"> <p>ทดสอบยิงรหัส: 111, 222 หรือ 333</p> </div> </div> <div class="cart-box"> <h3>รายการสินค้าที่ขาย</h3> <table id="cart-table"> <thead> <tr> <th>สินค้า</th> <th>ราคา</th> <th>เวลา</th> </tr> </thead> <tbody> </tbody> </table> <div class="total-section"> รวม: <span id="grand-total">0.00</span> บาท </div> <button onclick="location.reload()" style="margin-top:20px; width:100%; padding:10px; cursor:pointer;">ล้างหน้าจอ (จบการขาย)</button> </div> </div> <script> const barcodeInput = document.getElementById('barcode'); const msgDiv = document.getElementById('msg'); const cartTable = document.getElementById('cart-table').getElementsByTagName('tbody')[0]; const totalDisplay = document.getElementById('grand-total'); let totalAll = 0; // ล็อคเป้าให้ Focus ที่ช่อง input ตลอดเวลา document.addEventListener('click', () => barcodeInput.focus()); barcodeInput.addEventListener('keypress', function(e) { if (e.key === 'Enter') { const code = this.value; if (code.trim() === '') return; // ส่งข้อมูลไปหลังบ้าน const formData = new FormData(); formData.append('barcode', code); fetch('process.php', { method: 'POST', body: formData }) .then(res => res.json()) .then(data => { if (data.status === 'success') { // 1. เพิ่มแถวในตาราง let row = cartTable.insertRow(0); // เพิ่มรายการล่าสุดไว้บนสุด row.insertCell(0).innerText = data.name; row.insertCell(1).innerText = data.price.toFixed(2); row.insertCell(2).innerText = new Date().toLocaleTimeString(); // 2. คำนวณราคารวม totalAll += data.price; totalDisplay.innerText = totalAll.toLocaleString(undefined, {minimumFractionDigits: 2}); // 3. แจ้งสถานะ msgDiv.style.color = 'green'; msgDiv.innerText = '✔ ' + data.name; } else { msgDiv.style.color = 'red'; msgDiv.innerText = '❌ ' + data.message; } }) .catch(err => console.error('Error:', err)); // ล้างค่ารอชิ้นต่อไป this.value = ''; } }); </script> <hr> <center> <a href="manage_products.php"> จัดการสินค้า </a> || <a href="report.php"> report </a> </center> </body> </html> |
3.process.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 |
<?php include 'db.php'; header('Content-Type: application/json'); if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['barcode'])) { $barcode = $_POST['barcode']; // ค้นหาสินค้า $stmt = $pdo->prepare("SELECT * FROM products WHERE barcode = ?"); $stmt->execute([$barcode]); $product = $stmt->fetch(); if ($product) { if ($product['stock'] > 0) { // บันทึกการขาย $ins = $pdo->prepare("INSERT INTO sales (product_id, quantity, total_price) VALUES (?, 1, ?)"); $ins->execute([$product['id'], $product['price']]); // ตัดสต็อก $upd = $pdo->prepare("UPDATE products SET stock = stock - 1 WHERE id = ?"); $upd->execute([$product['id']]); echo json_encode([ 'status' => 'success', 'name' => $product['name'], 'price' => (float)$product['price'] ]); } else { echo json_encode(['status' => 'error', 'message' => 'สินค้าหมด!']); } } else { echo json_encode(['status' => 'error', 'message' => 'ไม่พบรหัสบาร์โค้ดนี้']); } exit; } ?> |
4.manage_products.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 |
<?php include 'db.php'; // ระบบเพิ่มสินค้า if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_product'])) { $stmt = $pdo->prepare("INSERT INTO products (barcode, name, price, stock) VALUES (?, ?, ?, ?)"); $stmt->execute([$_POST['barcode'], $_POST['name'], $_POST['price'], $_POST['stock']]); header("Location: manage_products.php"); } ?> <!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <title>จัดการสต็อกสินค้า</title> <style> body { font-family: sans-serif; padding: 20px; background: #f4f4f4; } .container { max-width: 800px; margin: auto; background: white; padding: 20px; border-radius: 8px; } input { padding: 8px; margin: 5px; border: 1px solid #ddd; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 10px; text-align: left; } th { background: #eee; } .btn { padding: 10px 20px; background: #28a745; color: white; border: none; cursor: pointer; } </style> </head> <body> <div class="container"> <h2>📦 จัดการสต็อกสินค้า</h2> <a href="index.php">⬅ กลับไปหน้าขาย</a> <form method="POST" style="margin-top: 20px; background: #e9ecef; padding: 15px;"> <h4>เพิ่มสินค้าใหม่</h4> <input type="text" name="barcode" placeholder="รหัสบาร์โค้ด" required> <input type="text" name="name" placeholder="ชื่อสินค้า" required> <input type="number" step="0.01" name="price" placeholder="ราคา" required> <input type="number" name="stock" placeholder="จำนวนสต็อก" required> <button type="submit" name="add_product" class="btn">บันทึก</button> </form> <table> <tr> <th>บาร์โค้ด</th> <th>ชื่อสินค้า</th> <th>ราคา</th> <th>คงเหลือ</th> </tr> <?php $stmt = $pdo->query("SELECT * FROM products ORDER BY id DESC"); while ($row = $stmt->fetch()) { echo "<tr> <td>{$row['barcode']}</td> <td>{$row['name']}</td> <td>{$row['price']}</td> <td>{$row['stock']}</td> </tr>"; } ?> </table> </div> </body> </html> |
5. report.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 |
<?php include 'db.php'; ?> <!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <title>รายงานยอดขาย</title> <style> body { font-family: sans-serif; padding: 20px; } .report-card { background: #007bff; color: white; padding: 20px; border-radius: 8px; display: inline-block; margin-bottom: 20px; } table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid #ddd; padding: 10px; } </style> </head> <body> <h2>📊 รายงานสรุปยอดขาย</h2> <a href="index.php">กลับหน้าขาย</a> | <a href="manage_products.php">จัดการสต็อก</a> <br><br> <?php // คำนวณยอดขายรวมวันนี้ $stmt = $pdo->query("SELECT SUM(total_price) as grand_total FROM sales WHERE DATE(sale_date) = CURDATE()"); $res = $stmt->fetch(); ?> <div class="report-card"> <h3>ยอดขายวันนี้</h3> <h1>฿ <?php echo number_format($res['grand_total'] ?? 0, 2); ?></h1> </div> <h3>ประวัติการขายรายรายการ</h3> <table> <thead> <tr> <th>เวลา</th> <th>สินค้า</th> <th>ราคา</th> </tr> </thead> <tbody> <?php $stmt = $pdo->query("SELECT s.*, p.name FROM sales s JOIN products p ON s.product_id = p.id ORDER BY s.sale_date DESC"); while ($row = $stmt->fetch()) { echo "<tr> <td>{$row['sale_date']}</td> <td>{$row['name']}</td> <td>{$row['total_price']}</td> </tr>"; } ?> </tbody> </table> </body> </html> |
ร่วมสนับสนุน ค่ากาแฟ ค่าโฮส devbanban.com ได้ที่
ธนาคารกรุงไทย
ชื่อบัญชี นายพิศิษฐ์ บวรเลิศสุธี เลขที่ 878-0-17747-6
————————————————————————————
ธนาคารไทยพาณิชย์
ชื่อบัญชี นายพิศิษฐ์ บวรเลิศสุธี เลขที่ 406-359094-1
————————————————————————————
ธนาคารกสิกร
ชื่อบัญชี นายพิศิษฐ์ บวรเลิศสุธี เลขที่ 048-1-17571-2
fanpage : https://www.facebook.com/sornwebsites/