D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
public_html
/
encoadminders
/
Filename :
edit_blog.php
back
Copy
<?php // Include database connection include 'config.php'; // Fetch the blog ID from the URL parameter if (isset($_GET['id'])) { $blogId = $_GET['id']; // Get the existing blog data from the database $query = "SELECT * FROM blogs WHERE id = ?"; $stmt = $conn->prepare($query); $stmt->bind_param('i', $blogId); $stmt->execute(); $result = $stmt->get_result(); $blog = $result->fetch_assoc(); // Check if the blog exists if (!$blog) { echo "<script>alert('Blog not found!'); window.location.href='blogs.php';</script>"; exit; } } // Handle form submission for updating the blog if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_blog'])) { $title = $_POST['title']; $shortDesc = $_POST['short_desc']; $content = $_POST['content']; // Get the content from the hidden input // Handle image upload (if a new image is uploaded) if ($_FILES['image']['error'] == 0) { $imageName = $_FILES['image']['name']; $imageTmpName = $_FILES['image']['tmp_name']; $uploadDir = 'uploads/'; $imagePath = $uploadDir . basename($imageName); move_uploaded_file($imageTmpName, $imagePath); } else { // Use the old image path if no new image is uploaded $imagePath = $blog['image']; } // Update the blog in the database $query = "UPDATE blogs SET title = ?, short_desc = ?, content = ?, image = ? WHERE id = ?"; $stmt = $conn->prepare($query); $stmt->bind_param('ssssi', $title, $shortDesc, $content, $imagePath, $blogId); if ($stmt->execute()) { echo "<script>alert('Blog updated successfully!'); window.location.href='blogs.php';</script>"; } else { echo "<script>alert('Error updating blog!');</script>"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <?php include('style.php') ?> <title>Edit Blog</title> </head> <body> <?php include('sidebar.php') ?> <div class="home-section"> <div class="container"> <h2>Edit Blog</h2> <!-- Edit Blog Form --> <form method="POST" enctype="multipart/form-data" style="margin-top: 20px;" id="edit-blog-form"> <!-- Title Input --> <div class="form-group"> <label for="title">Blog Title</label> <input type="text" name="title" id="title" class="form-control" placeholder="Enter blog title" value="<?php echo htmlspecialchars($blog['title']); ?>" required> </div> <!-- Short Description Input --> <div class="form-group"> <label for="short_desc">Short Description</label> <textarea name="short_desc" id="short_desc" class="form-control" placeholder="Enter short description" rows="3" required><?php echo htmlspecialchars($blog['short_desc']); ?></textarea> </div> <!-- Content Editable Section --> <div class="toolbar"> <!-- Toolbar for formatting text --> <div class="head"> <input type="text" placeholder="Filename" value="untitled" id="filename"> <select onchange="fileHandle(this.value); this.selectedIndex=0"> <option value="" selected="" hidden="" disabled="">File</option> <option value="new">New file</option> <option value="txt">Save as txt</option> <option value="pdf">Save as pdf</option> </select> <select onchange="formatDoc('formatBlock', this.value); this.selectedIndex=0;"> <option value="" selected="" hidden="" disabled="">Format</option> <option value="h1">Heading 1</option> <option value="h2">Heading 2</option> <option value="h3">Heading 3</option> <option value="h4">Heading 4</option> <option value="h5">Heading 5</option> <option value="h6">Heading 6</option> <option value="p">Paragraph</option> </select> <select onchange="formatDoc('fontSize', this.value); this.selectedIndex=0;"> <option value="" selected="" hidden="" disabled="">Font size</option> <option value="1">Extra small</option> <option value="2">Small</option> <option value="3">Regular</option> <option value="4">Medium</option> <option value="5">Large</option> <option value="6">Extra Large</option> <option value="7">Big</option> </select> <div class="color"> <span>Color</span> <input type="color" oninput="formatDoc('foreColor', this.value);"> </div> <div class="color"> <span>Background</span> <input type="color" oninput="formatDoc('hiliteColor', this.value);"> </div> </div> <!-- Toolbar buttons --> <div class="btn-toolbar"> <button type="button" onclick="formatDoc('undo')"><i class='bx bx-undo'></i></button> <button type="button" onclick="formatDoc('redo')"><i class='bx bx-redo'></i></button> <button type="button" onclick="formatDoc('bold')"><i class='bx bx-bold'></i></button> <button type="button" onclick="formatDoc('underline')"><i class='bx bx-underline'></i></button> <button type="button" onclick="formatDoc('italic')"><i class='bx bx-italic'></i></button> <button type="button" onclick="formatDoc('strikeThrough')"><i class='bx bx-strikethrough'></i></button> <button type="button" onclick="formatDoc('justifyLeft')"><i class='bx bx-align-left'></i></button> <button type="button" onclick="formatDoc('justifyCenter')"><i class='bx bx-align-middle'></i></button> <button type="button" onclick="formatDoc('justifyRight')"><i class='bx bx-align-right'></i></button> <button type="button" onclick="formatDoc('justifyFull')"><i class='bx bx-align-justify'></i></button> <button type="button" onclick="formatDoc('insertOrderedList')"><i class='bx bx-list-ol'></i></button> <button type="button" onclick="formatDoc('insertUnorderedList')"><i class='bx bx-list-ul'></i></button> <button type="button" onclick="addLink()"><i class='bx bx-link'></i></button> <button type="button" onclick="formatDoc('unlink')"><i class='bx bx-unlink'></i></button> </div> </div> <!-- Content Section --> <div id="content" name="content" contenteditable="true" spellcheck="false" style="border: 1px solid #ccc; padding: 10px; min-height: 150px;"> <?php echo $blog['content']; ?> </div> <!-- Hidden input to store content --> <input type="hidden" name="content" id="content-input"> <!-- Image Upload --> <div class="form-group" style="margin-top: 20px;"> <label for="image">Featured Image</label> <input type="file" name="image" id="image" class="form-control-file"> <img src="<?php echo $blog['image']; ?>" width="100" alt="Current Image" style="margin-top: 10px;"> </div> <!-- Submit Button --> <div class="form-group" style="margin-top: 20px;"> <button type="submit" name="update_blog" class="btn btn-primary">Update Blog</button> </div> </form> </div> </div> <?php include('footer.php') ?> <!-- JavaScript to handle content before form submission --> <script> document.getElementById('edit-blog-form').addEventListener('submit', function () { // Get the content of the contenteditable div var content = document.getElementById('content').innerHTML; // Set the value of the hidden input field to the content of the div document.getElementById('content-input').value = content; }); function formatDoc(cmd, value = null) { document.execCommand(cmd, false, value); } function addLink() { const url = prompt("Enter the URL:", "http://"); if (url) { formatDoc('createLink', url); } } </script> </body> </html>