File manager - Edit - /home/webapp69.cm.in.th/u69319090041/Shop41/public/register_store.php
Back
<?php /** * public/register_store.php — สมัครเปิดร้านค้า (Register Store / Apply Vendor) */ require_once __DIR__ . '/../config/config.php'; require_once __DIR__ . '/../includes/auth.php'; // ตรวจสอบว่าเปิดระบบล็อกอินหรือยัง ถ้ายังให้ Redirect ไปหน้า Login if (!isLoggedIn()) { header("Location: login.php?redirect=register_store.php"); exit; } $db = getDBConnection(); $userId = $_SESSION['user']['id'] ?? $_SESSION['user_id'] ?? null; if (!$userId) { header("Location: login.php?redirect=register_store.php"); exit; } // 1. ตรวจสอบว่าผู้ใช้งานนี้มีร้านค้าเดิมอยู่แล้วหรือไม่ (1 User = 1 Store) $stmt = $db->prepare("SELECT id, store_name, status FROM vendors WHERE user_id = ?"); $stmt->execute([$userId]); $existingStore = $stmt->fetch(); if ($existingStore) { if ($existingStore['status'] === 'pending') { $error = "คุณได้ส่งคำขอเปิดร้านค้า **'" . htmlspecialchars($existingStore['store_name']) . "'** แล้ว ขณะนี้อยู่ในระหว่างรอ Admin อนุมัติครับ ⏳"; } else if ($existingStore['status'] === 'approved') { $error = "คุณมีร้านค้า **'" . htmlspecialchars($existingStore['store_name']) . "'** ในระบบอยู่แล้วครับ 🏪"; } else { $error = "ร้านค้าของคุณถูกระงับการใช้งาน กรุณาติดต่อผู้ดูแลระบบครับ"; } } // 2. ประมวลผลเมื่อส่งฟอร์ม (POST Request) if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$existingStore) { $storeName = trim($_POST['store_name'] ?? ''); $description = trim($_POST['description'] ?? ''); $phone = trim($_POST['phone'] ?? ''); // Validations if (empty($storeName)) { $error = "กรุณาระบุชื่อร้านค้า"; } else if (empty($phone)) { $error = "กรุณาระบุเบอร์โทรศัพท์ติดต่อ"; } else { // จัดการอัปโหลดไฟล์รูปภาพโลโก้ร้านค้า $logoPath = null; if (isset($_FILES['store_logo']) && $_FILES['store_logo']['error'] === UPLOAD_ERR_OK) { $fileTmpPath = $_FILES['store_logo']['tmp_name']; $fileName = $_FILES['store_logo']['name']; $fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); $allowedExtensions = ['jpg', 'jpeg', 'png', 'webp', 'gif']; if (in_array($fileExtension, $allowedExtensions)) { // สร้างโฟลเดอร์ uploads/stores/ ถ้ายังไม่มี $uploadDir = __DIR__ . '/uploads/stores/'; if (!is_dir($uploadDir)) { mkdir($uploadDir, 0755, true); } // สุ่มชื่อไฟล์ป้องกันชื่อซ้ำ $newFileName = 'store_' . $userId . '_' . time() . '_' . bin2hex(random_bytes(4)) . '.' . $fileExtension; $destPath = $uploadDir . $newFileName; if (move_uploaded_file($fileTmpPath, $destPath)) { $logoPath = 'uploads/stores/' . $newFileName; } else { $error = "ไม่สามารถบันทึกไฟล์รูปภาพได้ กรุณาลองใหม่อีกครั้ง"; } } else { $error = "อนุญาตเฉพาะไฟล์รูปภาพประเภท JPG, PNG, WEBP หรือ GIF เท่านั้น"; } } // หากไม่มีข้อผิดพลาด ให้บันทึกลง Database if (empty($error)) { // สร้าง slug จากชื่อร้านค้า $storeSlug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $storeName))); if (empty($storeSlug)) { $storeSlug = 'store-' . $userId . '-' . time(); } try { // เช็คคอลัมน์ในตาราง vendors ว่ามี phone หรือไม่ $colCheck = $db->query("SHOW COLUMNS FROM vendors LIKE 'phone'")->fetch(); if (!$colCheck) { // หากยังไม่มีคอลัมน์ phone ให้สร้างให้อัตโนมัติ $db->exec("ALTER TABLE vendors ADD COLUMN phone VARCHAR(50) NULL AFTER store_logo"); } // Insert ลงตาราง vendors ด้วยสถานะ 'pending' $insertStmt = $db->prepare(" INSERT INTO vendors (user_id, store_name, store_slug, description, store_logo, phone, status, created_at) VALUES (?, ?, ?, ?, ?, ?, 'pending', NOW()) "); $insertStmt->execute([ $userId, $storeName, $storeSlug, $description, $logoPath, $phone ]); $success = "ส่งข้อมูลคำขอเปิดร้านค้าเรียบร้อยแล้ว! 🚀 กรุณารอ Admin ตรวจสอบและอนุมัติร้านค้าของคุณครับ"; // โหลดข้อมูลล่าสุด $existingStore = [ 'store_name' => $storeName, 'status' => 'pending' ]; } catch (PDOException $e) { $error = "เกิดข้อผิดพลาดในการบันทึกข้อมูล: " . $e->getMessage(); } } } } $pageTitle = "สมัครเปิดร้านค้า - " . SITE_NAME; require_once __DIR__ . '/../includes/header.php'; ?> <div class="container py-4"> <div class="row justify-content-center"> <div class="col-md-8 col-lg-6"> <div class="card card-custom shadow-lg border-0 rounded-4" style="background: var(--card-bg, #1e293b); color: var(--text-color, #f8fafc);"> <div class="card-header bg-transparent border-bottom text-center py-4"> <h3 class="fw-bold mb-1">🏪 เปิดร้านค้าของคุณบน <?php echo SITE_NAME; ?></h3> <p class="text-muted small mb-0">เริ่มต้นเป็นแม่ค้า/พ่อค้าออนไลน์และลงขายสินค้าได้ง่ายๆ</p> </div> <div class="card-body p-4"> <?php if (!empty($error)): ?> <div class="alert alert-warning border-0 shadow-sm rounded-3 mb-4"> ⚠️ <?php echo $error; ?> </div> <?php endif; ?> <?php if (!empty($success)): ?> <div class="alert alert-success border-0 shadow-sm rounded-3 mb-4"> ✅ <?php echo $success; ?> </div> <div class="text-center mt-4"> <a href="vendors.php" class="btn btn-primary rounded-pill px-4">ดูหน้าร้านค้าทั้งหมด</a> </div> <?php elseif ($existingStore): ?> <div class="text-center py-3"> <a href="vendors.php" class="btn btn-outline-light rounded-pill px-4">← กลับไปหน้าร้านค้าทั้งหมด</a> </div> <?php else: ?> <form method="POST" action="register_store.php" enctype="multipart/form-data"> <!-- ชื่อร้านค้า --> <div class="mb-3"> <label for="store_name" class="form-label fw-bold">ชื่อร้านค้า (Store Name) <span class="text-danger">*</span></label> <input type="text" class="form-control rounded-3 bg-dark text-white border-secondary" id="store_name" name="store_name" placeholder="เช่น 41Fashion Shop" required> </div> <!-- เบอร์โทรศัพท์ --> <div class="mb-3"> <label for="phone" class="form-label fw-bold">เบอร์โทรศัพท์ติดต่อ (Phone Number) <span class="text-danger">*</span></label> <input type="tel" class="form-control rounded-3 bg-dark text-white border-secondary" id="phone" name="phone" placeholder="เช่น 0812345678" required> </div> <!-- รายละเอียดร้านค้า --> <div class="mb-3"> <label for="description" class="form-label fw-bold">รายละเอียดร้านค้า (Description)</label> <textarea class="form-control rounded-3 bg-dark text-white border-secondary" id="description" name="description" rows="3" placeholder="อธิบายเกี่ยวกับร้านค้าและสินค้าของคุณ..."></textarea> </div> <!-- รูปโลโก้ร้านค้า --> <div class="mb-4"> <label for="store_logo" class="form-label fw-bold">รูปโลโก้ร้านค้า (Store Logo)</label> <input type="file" class="form-control rounded-3 bg-dark text-white border-secondary" id="store_logo" name="store_logo" accept="image/*"> <div class="form-text text-muted small">รองรับไฟล์ภาพ JPG, PNG, WEBP (แนะนำขนาดจัตุรัส)</div> </div> <!-- ปุ่มส่งข้อมูล --> <button type="submit" class="btn btn-success w-100 py-3 rounded-pill fw-bold fs-6 shadow-sm"> 🚀 ยืนยันการเปิดร้านค้า (Submit Application) </button> </form> <?php endif; ?> </div> </div> </div> </div> </div> <?php require_once __DIR__ . '/../includes/footer.php'; ?>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.22 |
proxy
|
phpinfo
|
Settings