0) {
$stmt = $conn->prepare("SELECT * FROM banners WHERE id=? LIMIT 1");
$stmt->bind_param("i", $editId);
} else {
$stmt = $conn->prepare("SELECT * FROM banners WHERE is_active=1 ORDER BY sort_order ASC, id ASC LIMIT 1");
}
$stmt->execute();
$record = $stmt->get_result()->fetch_assoc();
$stmt->close();
if (!$record) {
// 如果还没有任何记录,给出提示(你也可以在此创建一条空记录)
echo "
没有可编辑的 Banner,请先插入一条。
";
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = intval($_POST['id']);
$title_black = trim($_POST['title_black']);
$title_blue = trim($_POST['title_blue']);
$subtitle_text = trim($_POST['subtitle_text']);
$btn1_text = trim($_POST['btn1_text']);
$btn1_link = trim($_POST['btn1_link']);
$btn2_text = trim($_POST['btn2_text']);
$btn2_link = trim($_POST['btn2_link']);
$sort_order = intval($_POST['sort_order']);
$is_active = isset($_POST['is_active']) ? 1 : 0;
// 1) 默认沿用旧图
$imagePathForDB = $record['image_path'] ?? '';
// 2) 仅当上传了新文件且成功时才覆盖
if (!empty($_FILES['image']['name']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
// 存数据库用的 URL 相对路径(前端
![]()
直接用)
$uploadUrlDir = "/uploads/banners/"; // 放在站点根下,避免 open_basedir 问题
// 真实文件系统路径
$docRoot = rtrim($_SERVER['DOCUMENT_ROOT'], '/'); // e.g. /www/wwwroot/your-site
$uploadFsDir = $docRoot . $uploadUrlDir; // e.g. /www/wwwroot/your-site/uploads/banners/
if (!is_dir($uploadFsDir)) mkdir($uploadFsDir, 0777, true);
$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
$safe = preg_replace('/[^a-zA-Z0-9_\-\.]/', '_', pathinfo($_FILES['image']['name'], PATHINFO_FILENAME));
$newName = date("Ymd_His") . "_" . $safe . ($ext ? "." . $ext : "");
$targetFs = $uploadFsDir . $newName; // 磁盘路径
$targetUrl= $uploadUrlDir . $newName; // 存DB的相对URL
// 可选:MIME 白名单
// $finfo = finfo_open(FILEINFO_MIME_TYPE);
// $mime = finfo_file($finfo, $_FILES['image']['tmp_name']);
// if (!in_array($mime, ['image/png','image/jpeg','image/gif','image/webp'])) { die('不支持的图片类型'); }
if (is_uploaded_file($_FILES['image']['tmp_name']) && move_uploaded_file($_FILES['image']['tmp_name'], $targetFs)) {
// 删除旧图(仅删除站内文件)
if (!empty($record['image_path'])) {
$oldUrl = $record['image_path']; // 例如 /uploads/banners/xxx.png
$oldFs = $docRoot . $oldUrl;
if (is_file($oldFs)) { @unlink($oldFs); }
}
$imagePathForDB = $targetUrl;
} else {
echo "
图片上传失败。
";
}
}
// 3) 更新数据库
$sql = "UPDATE banners
SET image_path=?, title_black=?, title_blue=?, subtitle_text=?,
btn1_text=?, btn1_link=?, btn2_text=?, btn2_link=?,
sort_order=?, is_active=?
WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param(
"ssssssssiii",
$imagePathForDB, $title_black, $title_blue, $subtitle_text,
$btn1_text, $btn1_link, $btn2_text, $btn2_link,
$sort_order, $is_active, $id
);
if ($stmt->execute()) {
echo "
✅ Banner 更新成功
";
// 刷新 $record 显示最新
$stmt->close();
$stmt = $conn->prepare("SELECT * FROM banners WHERE id=? LIMIT 1");
$stmt->bind_param("i", $id);
$stmt->execute();
$record = $stmt->get_result()->fetch_assoc();
} else {
echo "
❌ 更新失败:" . htmlspecialchars($stmt->error) . "
";
}
$stmt->close();
}
?>