192 lines
9.1 KiB
PHP
192 lines
9.1 KiB
PHP
![]() |
<?php include('container/security.php');?>
|
||
|
<!DOCTYPE html>
|
||
|
<html lang="en" data-theme="light">
|
||
|
<head>
|
||
|
<!-- head links start -->
|
||
|
<?php include 'container/head_links.php';?>
|
||
|
<!-- head links start -->
|
||
|
<script src="https://cdn.ckeditor.com/4.22.1/standard/ckeditor.js"></script>
|
||
|
</head>
|
||
|
<body>
|
||
|
<!-- side_bar start -->
|
||
|
<?php include 'container/side_bar.php' ?>
|
||
|
<!-- side_bar end -->
|
||
|
<main class="dashboard-main">
|
||
|
<!-- Header start -->
|
||
|
<?php include 'container/header.php' ?>
|
||
|
<div class="dashboard-main-body">
|
||
|
<div class="d-flex flex-wrap align-items-center justify-content-between gap-3 mb-24">
|
||
|
<h6 class="fw-semibold mb-0">Manage AI Tool Blogs</h6>
|
||
|
<ul class="d-flex align-items-center gap-2">
|
||
|
<li class="fw-medium">
|
||
|
<a href="index-2.html" class="d-flex align-items-center gap-1 hover-text-primary">
|
||
|
<iconify-icon icon="solar:home-smile-angle-outline" class="icon text-lg"></iconify-icon>
|
||
|
Dashboard
|
||
|
</a>
|
||
|
</li>
|
||
|
<li>-</li>
|
||
|
<li class="fw-medium">Add Blog</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<?php
|
||
|
$blog_id = isset($_GET['id']) ? mysqli_real_escape_string($conn, $_GET['id']) : '';
|
||
|
if ($blog_id) {
|
||
|
// Fetch blog details
|
||
|
$query = "SELECT title, image, tags, status, description FROM blogs WHERE id='$blog_id'";
|
||
|
$result = mysqli_query($conn, $query);
|
||
|
|
||
|
if ($result && mysqli_num_rows($result) > 0) {
|
||
|
$blog = mysqli_fetch_assoc($result);
|
||
|
$title = htmlspecialchars($blog['title']);
|
||
|
$image_name = htmlspecialchars($blog['image']);
|
||
|
$tags = htmlspecialchars($blog['tags']);
|
||
|
$description = htmlspecialchars($blog['description']);
|
||
|
} else {
|
||
|
echo "<script>alert('Blog not found.'); window.location.href = 'blog_view';</script>";
|
||
|
exit();
|
||
|
}
|
||
|
} else {
|
||
|
echo "<script>alert('Invalid request.'); window.location.href = 'blog_view';</script>";
|
||
|
exit();
|
||
|
}
|
||
|
?>
|
||
|
<div class="row gy-4">
|
||
|
<div class="col-md-12">
|
||
|
<div class="card">
|
||
|
<div class="card-header">
|
||
|
<h5 class="card-title mb-0">Add New Blog</h5>
|
||
|
</div>
|
||
|
<div class="card-body">
|
||
|
<form action="" method="POST" enctype="multipart/form-data">
|
||
|
<div class="row gy-3">
|
||
|
<div class="col-12">
|
||
|
<label class="form-label">Blog Title</label>
|
||
|
<input type="text" name="title" class="form-control" required placeholder="Enter Blog Title" value="<?php echo $title; ?>">
|
||
|
</div>
|
||
|
<div class="col-md-6">
|
||
|
<label class="form-label">Blog Image</label>
|
||
|
<input type="file" name="image" class="form-control" accept="image/*">
|
||
|
<?php if ($image_name): ?>
|
||
|
<img src="assets/images/blogs_image/<?php echo $image_name; ?>" alt="Current Image" style="max-width: 100px; margin-top: 10px;">
|
||
|
<?php endif; ?>
|
||
|
</div>
|
||
|
<div class="col-md-6">
|
||
|
<label class="form-label">Tags</label>
|
||
|
<input type="text" name="tags" class="form-control" required placeholder="Enter Tags" value="<?php echo $tags; ?>">
|
||
|
</div>
|
||
|
<div class="col-md-6">
|
||
|
<label class="form-label">Status</label>
|
||
|
<select name="status" class="form-control" required>
|
||
|
<option value="0" <?php echo ($blog['status'] == 0) ? 'selected' : ''; ?>>未上架</option>
|
||
|
<option value="1" <?php echo ($blog['status'] == 1) ? 'selected' : ''; ?>>上架</option>
|
||
|
</select>
|
||
|
</div>
|
||
|
<div class="col-12">
|
||
|
<label class="form-label">Long Description</label>
|
||
|
<textarea name="editor1" id="editor1" required><?php echo $description; ?></textarea>
|
||
|
</div>
|
||
|
<input type="hidden" name="blog_id" value="<?php echo $blog_id; ?>">
|
||
|
<div class="col-12">
|
||
|
<button type="submit" class="btn btn-primary mt-3">Update Blog</button>
|
||
|
</div>
|
||
|
</div>
|
||
|
</form>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
|
||
|
<?php include 'container/footer.php' ?>
|
||
|
</main>
|
||
|
<!-- Footer Links Start -->
|
||
|
<?php include 'container/footer_links.php' ?>
|
||
|
<script>
|
||
|
// Replace textarea with CKEditor
|
||
|
CKEDITOR.replace('editor1');
|
||
|
</script>
|
||
|
<script>
|
||
|
// Function to perform the delayed click
|
||
|
function clickNotificationClose() {
|
||
|
setTimeout(function() {
|
||
|
var closeButton = document.querySelector('.cke_notification_close');
|
||
|
if (closeButton) {
|
||
|
closeButton.click();
|
||
|
} else {
|
||
|
console.log('Close button not found.');
|
||
|
}
|
||
|
}, 100); // Delay in milliseconds (e.g., 2000ms = 2 seconds)
|
||
|
}
|
||
|
|
||
|
// Call the function after the page has loaded
|
||
|
window.addEventListener('load', clickNotificationClose);
|
||
|
</script>
|
||
|
</body>
|
||
|
</html>
|
||
|
<?php
|
||
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
||
|
$blog_id = mysqli_real_escape_string($conn, $_POST['blog_id']);
|
||
|
$title = mysqli_real_escape_string($conn, $_POST['title']);
|
||
|
$tags = mysqli_real_escape_string($conn, $_POST['tags']);
|
||
|
$description = mysqli_real_escape_string($conn, $_POST['editor1']);
|
||
|
$status = mysqli_real_escape_string($conn, $_POST['status']); // 获取status字段
|
||
|
|
||
|
// Initialize variables for new image
|
||
|
$new_file_name = null;
|
||
|
|
||
|
// File upload logic
|
||
|
if (!empty($_FILES['image']['name'])) {
|
||
|
$target_dir = "assets/images/blogs_image/";
|
||
|
$file_name = basename($_FILES["image"]["name"]);
|
||
|
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
|
||
|
$new_file_name = uniqid() . '.' . $file_extension;
|
||
|
$target_file = $target_dir . $new_file_name;
|
||
|
|
||
|
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
|
||
|
// Fetch the current image name from the database
|
||
|
$query = "SELECT image FROM blogs WHERE id='$blog_id'";
|
||
|
$result = mysqli_query($conn, $query);
|
||
|
if ($result && mysqli_num_rows($result) > 0) {
|
||
|
$row = mysqli_fetch_assoc($result);
|
||
|
$current_image_name = $row['image'];
|
||
|
$current_image_file = $target_dir . $current_image_name;
|
||
|
|
||
|
// Update the blog record with new image and status
|
||
|
$update_query = "UPDATE blogs SET title='$title', image='$new_file_name', tags='$tags', description='$description', status='$status' WHERE id='$blog_id'";
|
||
|
if (mysqli_query($conn, $update_query)) {
|
||
|
// Delete the old image file if it exists
|
||
|
if (file_exists($current_image_file)) {
|
||
|
unlink($current_image_file);
|
||
|
}
|
||
|
echo "<script>alert('Blog updated successfully.'); window.location.href = 'blog_view';</script>";
|
||
|
} else {
|
||
|
echo "<script>alert('Error: " . mysqli_error($conn) . "'); window.location.href = 'blog_edit.php?id=$blog_id';</script>";
|
||
|
}
|
||
|
} else {
|
||
|
echo "<script>alert('Blog not found.'); window.location.href = 'blog_view';</script>";
|
||
|
}
|
||
|
} else {
|
||
|
echo "<script>alert('Error uploading image.'); window.location.href = 'blog_edit.php?id=$blog_id';</script>";
|
||
|
}
|
||
|
} else {
|
||
|
// Update the blog record without changing the image
|
||
|
$update_query = "UPDATE blogs SET title='$title', tags='$tags', description='$description', status='$status' WHERE id='$blog_id'";
|
||
|
if (mysqli_query($conn, $update_query)) {
|
||
|
echo "<script>alert('Blog updated successfully.'); window.location.href = 'blog_view';</script>";
|
||
|
} else {
|
||
|
echo "<script>alert('Error: " . mysqli_error($conn) . "'); window.location.href = 'blog_edit.php?id=$blog_id';</script>";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mysqli_close($conn);
|
||
|
}
|
||
|
?>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|