⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.124
Server IP:
50.28.103.30
Server:
Linux host.jcukjv-lwsites.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
Server Software:
nginx/1.28.0
PHP Version:
8.3.12
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
www
/
wwwroot
/
china-democracyparty.com
/
5
/
View File Name :
edit-blog.php
<?php // edit_blog.php — 修改文章(带后台登录判断) // === 0. 登录验证 === session_start(); if (empty($_SESSION['admin_logged'])) { header("Location: admin.php"); exit; } // 1. 引入数据库 require_once __DIR__ . '/sql.php'; // 2. 获取要编辑的那条记录 id(来自链接:edit_blog.php?id=3) $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; if ($id <= 0) { echo "参数错误,缺少 id"; exit; } // 3. 如果是提交表单,则先进行更新 if ($_SERVER['REQUEST_METHOD'] === 'POST') { $id = (int)($_POST['id'] ?? 0); $title = $_POST['title'] ?? ''; $description = $_POST['description'] ?? ''; $content = $_POST['content'] ?? ''; // 旧的封面图路径(用于没上传新文件时继续使用旧值) $image = $_POST['old_image'] ?? ''; // 处理上传 image if (!empty($_FILES['image']['name'])) { $uploadDir = __DIR__ . '/uploads/'; if (!is_dir($uploadDir)) { mkdir($uploadDir, 0755, true); } $filename = time() . '_blog_' . basename($_FILES['image']['name']); $path = $uploadDir . $filename; if (move_uploaded_file($_FILES['image']['tmp_name'], $path)) { // 存到数据库里的路径,一般写相对路径 $image = 'uploads/' . $filename; } } // 更新到 blogs 表 $sql = "UPDATE blogs SET title = ?, description = ?, content = ?, image = ? WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param( "ssssi", $title, $description, $content, $image, $id ); if ($stmt->execute()) { header("Location: admin.php?msg=blog_updated"); exit; } else { $error = "更新失败: " . $conn->error; } } // 4. 每次加载页面(包括更新失败回显),都从数据库取最新数据 $sql = "SELECT * FROM blogs WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $id); $stmt->execute(); $data = $stmt->get_result()->fetch_assoc(); if (!$data) { echo "没有找到该文章"; exit; } ?> <!DOCTYPE html> <html> <head> <title>后台管理 - 修改文章</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script> </head> <body> <main> <div class="container"> <header class="d-flex flex-wrap align-items-center justify-content-center justify-content-md-between py-3 mb-4 border-bottom"> <a href="/" class="d-flex align-items-center col-md-3 mb-2 mb-md-0 text-dark text-decoration-none"> <span class="fs-4">Admin</span> </a> <ul class="nav col-12 col-md-auto mb-2 justify-content-center mb-md-0"> <li><a href="admin.php" class="nav-link px-2 link-secondary">管理中心</a></li> </ul> <div class="col-md-3 text-end"> <a href="add.php" class="btn btn-outline-primary me-2">Add blog</a> <a href="logout.php" class="btn btn-primary">log out</a> </div> </header> </div> <div class="container mt-4"> <?php if (!empty($error)): ?> <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div> <?php endif; ?> <h3 class="mb-3">修改文章</h3> <!-- 提交到当前页面 --> <form action="" method="post" enctype="multipart/form-data"> <!-- 隐藏ID --> <input type="hidden" name="id" value="<?= htmlspecialchars($data['id']) ?>"> <!-- 保存旧的 image 路径 --> <input type="hidden" name="old_image" value="<?= htmlspecialchars($data['image'] ?? '') ?>"> <!-- 标题 --> <div class="mb-3 mt-3"> <label class="form-label">标题(title)</label> <input type="text" class="form-control" name="title" value="<?= htmlspecialchars($data['title'] ?? '') ?>" required> </div> <!-- 描述 --> <div class="mb-3 mt-3"> <label class="form-label">描述(description)</label> <textarea class="form-control" rows="3" name="description" required><?= htmlspecialchars($data['description'] ?? '') ?></textarea> </div> <!-- 内容 --> <div class="mb-3 mt-3"> <label class="form-label">内容(content)</label> <textarea class="form-control" rows="8" name="content" required><?= htmlspecialchars($data['content'] ?? '') ?></textarea> </div> <!-- 封面图 --> <div class="mb-3 mt-3"> <label class="form-label">封面图片(image)</label><br> <?php if (!empty($data['image'])): ?> 当前:<img src="<?= htmlspecialchars($data['image']) ?>" style="max-width:150px;max-height:150px;object-fit:cover;"><br><br> <?php endif; ?> <input type="file" name="image"> <div class="form-text">如不上传新图片,将继续使用当前封面。</div> </div> <!-- 只展示时间和浏览次数,不修改 --> <div class="mb-3 mt-3"> <label class="form-label">创建时间(created_at)</label> <input type="text" class="form-control" value="<?= htmlspecialchars($data['created_at'] ?? '') ?>" disabled> </div> <div class="mb-3 mt-3"> <label class="form-label">浏览次数(views)</label> <input type="text" class="form-control" value="<?= (int)($data['views'] ?? 0) ?>" disabled> </div> <button type="submit" class="btn btn-primary">更新</button> </form> </div> </main> </body> </html>