main
xgkblog/admin/links.php
links.php10.4 KB
<?php
require 'config.php';
$page_title = "友情链接管理";

// ================= 1. 处理删除操作 =================
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $stmt = $pdo->prepare("DELETE FROM links WHERE id = ?");
    $stmt->execute([$id]);
    header('Location: links.php?msg=deleted'); exit;
}

// ================= 2. 处理表单提交 (新增 / 编辑) =================
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 判断是编辑还是新增:如果 POST 中带有 id 且大于0,则为编辑
    $id = intval($_POST['id'] ?? 0); 
    
    if ($id > 0) {
        // 【编辑模式】更新数据库
        $stmt = $pdo->prepare("UPDATE links SET name = :name, url = :url, description = :desc WHERE id = :id");
        $stmt->execute([
            ':name' => $_POST['name'],
            ':url'  => $_POST['url'],
            ':desc' => $_POST['description'],
            ':id'   => $id
        ]);
        header('Location: links.php?msg=updated'); exit;
    } else {
        // 【新增模式】插入数据库
        $stmt = $pdo->prepare("INSERT INTO links (name, url, description) VALUES (:name, :url, :desc)");
        $stmt->execute([
            ':name' => $_POST['name'],
            ':url'  => $_POST['url'],
            ':desc' => $_POST['description']
        ]);
        header('Location: links.php?msg=added'); exit;
    }
}

// ================= 3. 获取数据用于渲染 =================
// 3.1 获取所有友链列表
$stmt = $pdo->query("SELECT * FROM links ORDER BY id DESC");
$links = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 3.2 检查是否在编辑状态(URL 带有 ?edit=ID)
$editing_link = null;
if (isset($_GET['edit'])) {
    $edit_id = intval($_GET['edit']);
    $stmt = $pdo->prepare("SELECT * FROM links WHERE id = ?");
    $stmt->execute([$edit_id]);
    $editing_link = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($editing_link) {
        $page_title = "编辑友情链接"; // 动态修改页面标题
    }
}

ob_start(); 
?>




<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title><?php echo $page_title; ?></title>
    <style>
      
    </style>
</head>
<body>

    <div class="overlay" id="overlay"></div>

    <!-- 侧边栏 -->
    <?php require_once __DIR__ . '/views/sidebar.php'; ?>

    <!-- 主内容区 -->
    <div class="main-wrapper">
      <?php require_once __DIR__ . '/views/header.php'; ?>
        <main class="content-area">
            <div class="page-header">
                <h1 class="page-title"><?php echo $page_title; ?></h1>
                <button class="btn-primary"><a href="post_edit.php">+ 撰写新文章</a></button>
            </div>
            
            <!-- 统计卡片 -->
            <?php require_once __DIR__ . '/views/tj.php'; ?>

            <!-- 数据表格 -->
          <!-- 提示信息 -->
<style>
    /* ========== 友链表单卡片 ========== */
    .link-form-card {
        background: #ffffff;
        border-radius: 12px;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
        padding: 30px;
        margin-bottom: 30px;
        border: 1px solid #eef2f6;
    }
    .link-form-card h3 {
        margin: 0 0 25px 0;
        font-size: 20px;
        color: #2c3e50;
        padding-bottom: 15px;
        border-bottom: 2px solid #f0f4f8;
    }

    /* ========== 表单元素通用样式 ========== */
    .link-form-card .form-group { margin-bottom: 20px; }
    .link-form-card .form-group label {
        display: block;
        margin-bottom: 8px;
        font-weight: 600;
        color: #4a5568;
        font-size: 14px;
    }
    .link-form-card .form-control {
        width: 100%;
        padding: 12px 15px;
        border: 1px solid #d1d9e6;
        border-radius: 8px;
        font-size: 15px;
        color: #333;
        background-color: #fafbfc;
        transition: all 0.3s ease;
        box-sizing: border-box;
    }
    .link-form-card .form-control:focus {
        outline: none;
        border-color: #4a90e2;
        background-color: #fff;
        box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.1);
    }

    /* ========== 双列网格布局 ========== */
    .form-row {
        display: grid;
        grid-template-columns: 1fr 1fr; /* 默认两列 */
        gap: 20px; /* 两列之间的间距 */
    }
    @media (max-width: 600px) {
        .form-row {
            grid-template-columns: 1fr; /* 手机端自动变单列 */
        }
    }

    /* ========== 按钮组 ========== */
    .form-actions {
        margin-top: 25px;
        display: flex;
        align-items: center;
        gap: 12px;
    }
    .btn {
        padding: 12px 24px;
        border: none;
        border-radius: 8px;
        font-size: 15px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        text-decoration: none;
        display: inline-block;
        text-align: center;
    }
    .btn-primary {
        background: linear-gradient(135deg, #4a90e2, #357abd);
        color: #fff;
    }
    .btn-primary:hover { background: linear-gradient(135deg, #357abd, #2a629e); transform: translateY(-1px); }
    
    .btn-secondary {
        background: #f0f4f8;
        color: #5a6b7d;
        border: 1px solid #d1d9e6;
    }
    .btn-secondary:hover { background: #e4e9f0; }

    /* ========== 提示信息 ========== */
    .alert-success {
        background-color: #e6f9ee;
        color: #1e7e34;
        border: 1px solid #b2e6c4;
        padding: 12px 18px;
        border-radius: 8px;
        margin-bottom: 20px;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 8px;
    }
</style>

<!-- 提示信息 -->
<?php if(isset($_GET['msg'])): ?>
    <div class="alert-success">
        ✅ <?php echo $_GET['msg'] == 'updated' ? '友链已更新!' : ($_GET['msg'] == 'deleted' ? '友链已删除!' : '新友链已添加!'); ?>
    </div>
<?php endif; ?>

<!-- ================= 4. 统一表单 (新增 & 编辑共用) ================= -->
<div class="link-form-card">
    <h3>
        <?php echo $editing_link ? '✏️ 编辑友链' : '➕ 添加新友链'; ?>
    </h3>
    
    <form method="POST">
        <!-- 隐藏字段:用于在编辑时传递当前友链的 ID -->
        <input type="hidden" name="id" value="<?php echo htmlspecialchars($editing_link['id'] ?? 0); ?>">
        
        <!-- 双列布局:名称与链接 -->
        <div class="form-row">
            <div class="form-group">
                <label for="link-name">网站名称 <span style="color:red;">*</span></label>
                <input type="text" id="link-name" name="name" class="form-control" placeholder="例如:某某的博客" required 
                       value="<?php echo htmlspecialchars($editing_link['name'] ?? ''); ?>">
            </div>
            
            <div class="form-group">
                <label for="link-url">网站链接 <span style="color:red;">*</span></label>
                <input type="url" id="link-url" name="url" class="form-control" placeholder="https://..." required 
                       value="<?php echo htmlspecialchars($editing_link['url'] ?? ''); ?>">
            </div>
        </div>
        
        <div class="form-group">
            <label for="link-desc">网站描述</label>
            <input type="text" id="link-desc" name="description" class="form-control" placeholder="选填:一句话介绍..." 
                   value="<?php echo htmlspecialchars($editing_link['description'] ?? ''); ?>">
        </div>
        
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">
                <?php echo $editing_link ? '💾 保存修改' : '➕ 添加新友链'; ?>
            </button>
            
            <!-- 如果在编辑状态,提供一个取消按钮 -->
            <?php if ($editing_link): ?>
                <a href="links.php" class="btn btn-secondary">取消编辑</a>
            <?php endif; ?>
        </div>
    </form>
</div>

<!-- ================= 5. 友链列表表格 ================= -->
<div class="card" style="padding: 0; overflow: hidden;">
    <div class="table-wrapper">
        <table>
            <thead>
                <tr>
                    <th style="width: 40px;"><input type="checkbox" class="table-checkbox"></th>
                    <th style="width: 60px;">ID</th>
                    <th>名称</th>
                    <th>链接</th>
                    <th>描述</th>
                    <th style="width: 120px;">操作</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($links)): ?>
                    <?php foreach ($links as $link): ?>
                    <tr>
                        <td><input type="checkbox" class="table-checkbox"></td>
                        <td><?php echo $link['id']; ?></td>
                        <td>
                            <div class="article-title"><?php echo htmlspecialchars($link['name']); ?></div>
                        </td>
                        <td>
                            <div class="article-cat">
                                <a href="<?php echo htmlspecialchars($link['url']); ?>" target="_blank" style="color:#007bff; text-decoration:none;">
                                    <?php echo htmlspecialchars($link['url']); ?>
                                </a>
                            </div>
                        </td>
                        <td>
                            <div class="article-cat"><?php echo htmlspecialchars($link['description']); ?></div>
                        </td>
                        <td>
                            <!-- 编辑按钮 -->
                            <a href="?edit=<?php echo $link['id']; ?>" class="action-btn" title="编辑">✏️</a>
                            <!-- 删除按钮 -->
                            <a href="?delete=<?php echo $link['id']; ?>" class="action-btn" title="删除" onclick="return confirm('确定要删除这个友链吗?')">🗑️</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="6" style="text-align:center; color:#999; padding:30px 0;">暂无友情链接,请在上方添加。</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
          
        </main>
    </div>

    <?php require_once __DIR__ . '/views/footer.php'; ?>
</body>
</html>