File manager - Edit - /var/www/order.cmtc.ac.th/admin/products_2.php
Back
<?php session_start(); if(!isset($_SESSION['admin'])) { header('Location: index.php'); exit; } include('../config/db.php'); // ✅ เพิ่มสินค้าใหม่ if(isset($_POST['add'])) { $name = $conn->real_escape_string($_POST['name']); $price = floatval($_POST['price']); $stock = intval($_POST['stock']); $img1 = ""; $img2 = ""; if(!empty($_FILES['img1']['name'])) { $img1 = time().'_1_'.$_FILES['img1']['name']; move_uploaded_file($_FILES['img1']['tmp_name'], "../uploads/products/".$img1); } if(!empty($_FILES['img2']['name'])) { $img2 = time().'_2_'.$_FILES['img2']['name']; move_uploaded_file($_FILES['img2']['tmp_name'], "../uploads/products/".$img2); } $conn->query("INSERT INTO products(name, price, stock, img1, img2) VALUES('$name', $price, $stock, '$img1', '$img2')"); header("Location: products.php?added=1"); exit; } // ✅ แก้ไขสินค้า if(isset($_POST['update'])) { $id = intval($_POST['id']); $name = $conn->real_escape_string($_POST['name']); $price = floatval($_POST['price']); $stock = intval($_POST['stock']); $img_sql = ""; if(!empty($_FILES['img1']['name'])) { $img1 = time().'_1_'.$_FILES['img1']['name']; move_uploaded_file($_FILES['img1']['tmp_name'], "../uploads/products/".$img1); $img_sql .= ", img1='$img1'"; } if(!empty($_FILES['img2']['name'])) { $img2 = time().'_2_'.$_FILES['img2']['name']; move_uploaded_file($_FILES['img2']['tmp_name'], "../uploads/products/".$img2); $img_sql .= ", img2='$img2'"; } $conn->query("UPDATE products SET name='$name', price=$price, stock=$stock $img_sql WHERE id=$id"); header("Location: products.php?updated=1"); exit; } // ✅ ลบสินค้า if(isset($_GET['delete'])) { $id = intval($_GET['delete']); $conn->query("DELETE FROM products WHERE id=$id"); header("Location: products.php?deleted=1"); exit; } $res = $conn->query("SELECT * FROM products ORDER BY id DESC"); ?> <!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <title>จัดการรายการเหรียญ</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Kanit:wght@300;400;500;600&display=swap" rel="stylesheet"> <style> body,*{font-family:'Kanit',sans-serif!important;} img.thumb{width:80px;border-radius:8px;box-shadow:0 0 4px rgba(0,0,0,0.2);} </style> </head> <body class="bg-light"> <?php include("menu.php"); ?> <div class="container py-4"> <div class="d-flex justify-content-between align-items-center mb-3"> <h3>🪙 จัดการรายการเหรียญ</h3> <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal">➕ เพิ่มรายการเหรียญ</button> </div> <?php if(isset($_GET['added'])): ?> <div class="alert alert-success">✅ เพิ่มเหรียญใหม่เรียบร้อยแล้ว!</div> <?php elseif(isset($_GET['updated'])): ?> <div class="alert alert-info">🔄 แก้ไขข้อมูลเหรียญเรียบร้อยแล้ว!</div> <?php elseif(isset($_GET['deleted'])): ?> <div class="alert alert-danger">🗑️ ลบเหรียญเรียบร้อยแล้ว!</div> <?php endif; ?> <div class="card shadow-sm"> <div class="card-header bg-dark text-white fw-bold">📋 รายการเหรียญทั้งหมด</div> <div class="card-body"> <table class="table table-bordered align-middle text-center"> <thead class="table-light"> <tr> <th>ID</th> <th>ชื่อเหรียญ</th> <th>ราคา</th> <th>คงเหลือ</th> <th>ด้านหน้า</th> <th>ด้านหลัง</th> <th>จัดการ</th> </tr> </thead> <tbody> <?php if($res->num_rows > 0): ?> <?php while($row = $res->fetch_assoc()): ?> <tr> <td><?=$row['id']?></td> <td><?=$row['name']?></td> <td><?=number_format($row['price'],2)?></td> <td><?=$row['stock']?></td> <td><?php if($row['img1']) echo "<img src='../uploads/products/{$row['img1']}' class='thumb'>"; ?></td> <td><?php if($row['img2']) echo "<img src='../uploads/products/{$row['img2']}' class='thumb'>"; ?></td> <td> <button class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#edit<?=$row['id']?>">แก้ไข</button> <button class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#delete<?=$row['id']?>">ลบ</button> </td> </tr> <!-- 🔧 Modal แก้ไข --> <div class="modal fade" id="edit<?=$row['id']?>" tabindex="-1"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <form method="post" enctype="multipart/form-data"> <div class="modal-header bg-warning"> <h5 class="modal-title">✏️ แก้ไขข้อมูลเหรียญ</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <input type="hidden" name="id" value="<?=$row['id']?>"> <div class="row g-3"> <div class="col-md-6"> <label class="form-label">ชื่อเหรียญ</label> <input type="text" name="name" value="<?=$row['name']?>" class="form-control" required> </div> <div class="col-md-3"> <label class="form-label">ราคา (บาท)</label> <input type="number" name="price" step="0.01" value="<?=$row['price']?>" class="form-control" required> </div> <div class="col-md-3"> <label class="form-label">จำนวนคงเหลือ</label> <input type="number" name="stock" value="<?=$row['stock']?>" class="form-control" required> </div> <div class="col-md-6"> <label class="form-label">รูปด้านหน้า</label> <input type="file" name="img1" class="form-control"> <?php if($row['img1']): ?><img src="../uploads/products/<?=$row['img1']?>" class="thumb mt-2"><?php endif; ?> </div> <div class="col-md-6"> <label class="form-label">รูปด้านหลัง</label> <input type="file" name="img2" class="form-control"> <?php if($row['img2']): ?><img src="../uploads/products/<?=$row['img2']?>" class="thumb mt-2"><?php endif; ?> </div> </div> </div> <div class="modal-footer"> <button type="submit" name="update" class="btn btn-success">💾 บันทึกการแก้ไข</button> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">ปิด</button> </div> </form> </div> </div> </div> <!-- 🗑️ Modal ยืนยันการลบ --> <div class="modal fade" id="delete<?=$row['id']?>" tabindex="-1"> <div class="modal-dialog modal-dialog-centered"> <div class="modal-content"> <div class="modal-header bg-danger text-white"> <h5 class="modal-title">⚠️ ยืนยันการลบรายการเหรียญ</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body text-center"> <p>คุณแน่ใจหรือไม่ว่าต้องการลบรายการนี้?</p> <p class="fw-bold text-danger">"<?=$row['name']?>"</p> <p>การลบนี้ไม่สามารถย้อนกลับได้</p> </div> <div class="modal-footer justify-content-center"> <a href="?delete=<?=$row['id']?>" class="btn btn-danger">🗑️ ยืนยันการลบ</a> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">ยกเลิก</button> </div> </div> </div> </div> <?php endwhile; ?> <?php else: ?> <tr><td colspan="7" class="text-center text-muted">ยังไม่มีรายการเหรียญ</td></tr> <?php endif; ?> </tbody> </table> </div> </div> </div> <!-- ➕ Modal เพิ่มรายการเหรียญ --> <div class="modal fade" id="addModal" tabindex="-1"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <form method="post" enctype="multipart/form-data"> <div class="modal-header bg-primary text-white"> <h5 class="modal-title">➕ เพิ่มรายการเหรียญใหม่</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <div class="row g-3"> <div class="col-md-6"> <label class="form-label">ชื่อเหรียญ</label> <input type="text" name="name" class="form-control" required> </div> <div class="col-md-3"> <label class="form-label">ราคา (บาท)</label> <input type="number" name="price" step="0.01" class="form-control" required> </div> <div class="col-md-3"> <label class="form-label">จำนวนคงเหลือ</label> <input type="number" name="stock" class="form-control" required> </div> <div class="col-md-6"> <label class="form-label">รูปด้านหน้า</label> <input type="file" name="img1" class="form-control"> </div> <div class="col-md-6"> <label class="form-label">รูปด้านหลัง</label> <input type="file" name="img2" class="form-control"> </div> </div> </div> <div class="modal-footer"> <button type="submit" name="add" class="btn btn-success">💾 บันทึก</button> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">ปิด</button> </div> </form> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.41 |
proxy
|
phpinfo
|
Settings