您好!欢迎访问家园网-www.jy.wang!

家园网

admin/modules/product_add.php

网络 作者:本站 点击:
<?php
// 获取数据库实例
$db = DB::getInstance();
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 接收表单数据
    $name = $_POST['name'] ?? '';
    $price = floatval($_POST['price'] ?? 0);
    $description = $_POST['description'] ?? '';
    $content = $_POST['content'] ?? '';
    $sort = intval($_POST['sort'] ?? 50);
    $status = isset($_POST['status']) ? 1 : 0;
    
    // 处理图片上传
    $thumb = '';
    if (isset($_FILES['thumb']) && $_FILES['thumb']['error'] === 0) {
        $uploadDir = ROOT_PATH . 'uploads/product/';
        // 如果目录不存在,创建它
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0755, true);
        }
        
        $ext = pathinfo($_FILES['thumb']['name'], PATHINFO_EXTENSION);
        $filename = date('YmdHis') . '_' . mt_rand(1000, 9999) . '.' . $ext;
        $targetPath = $uploadDir . $filename;
        
        if (move_uploaded_file($_FILES['thumb']['tmp_name'], $targetPath)) {
            $thumb = 'uploads/product/' . $filename;
        }
    }
    
    // 插入数据库
    $sql = "INSERT INTO product (name, price, thumb, description, content, sort, status) 
            VALUES (?, ?, ?, ?, ?, ?, ?)";
    $result = $db->query($sql, [$name, $price, $thumb, $description, $content, $sort, $status]);
    
    if ($result) {
        header('Location: ?mod=product_list&msg=add_success');
        exit;
    } else {
        $error = '添加失败,请重试。';
    }
}
?>
<div class="module-header">
    <h3>添加产品</h3>
    <a href="?mod=product_list" class="btn-add" style="background:#6c757d;">返回列表</a>
</div>
<div class="module-body">
    <?php if (isset($error)): ?>
        <div class="alert error"><?php echo $error; ?></div>
    <?php endif; ?>
    
    <form method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label>产品名称 <span style="color:red;">*</span></label>
            <input type="text" name="name" required>
        </div>
        
        <div class="form-group">
            <label>价格</label>
            <input type="number" name="price" step="0.01" value="0.00">
        </div>
        
        <div class="form-group">
            <label>缩略图</label>
            <input type="file" name="thumb" accept="image/*">
            <small style="color:#999;">支持 jpg、png、gif,建议尺寸 400x300</small>
        </div>
        
        <div class="form-group">
            <label>简短描述</label>
            <textarea name="description" rows="3" placeholder="用于产品列表页的简短介绍..."></textarea>
        </div>
        
        <div class="form-group">
            <label>产品详情</label>
            <textarea name="content" rows="10" placeholder="详细的产品介绍,支持HTML..."></textarea>
            <small style="color:#999;">可以使用 HTML 标签,如 &lt;p&gt;、&lt;strong&gt;、&lt;img&gt; 等</small>
        </div>
        
        <div class="form-group">
            <label>排序</label>
            <input type="number" name="sort" value="50">
            <small>数字越小,显示越靠前</small>
        </div>
        
        <div class="form-group">
            <label>
                <input type="checkbox" name="status" checked> 上架
            </label>
        </div>
        
        <div class="form-group">
            <button type="submit" class="btn-save">保存产品</button>
            <a href="?mod=product_list" class="btn-add" style="background:#6c757d; text-decoration:none;">取消</a>
        </div>
    </form>
</div>


标签: