D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
public_html
/
encoadminders
/
Filename :
edit_testimonial.php
back
Copy
<?php session_start(); include 'config.php'; if (!isset($_SESSION['admin_name'])) { echo '<script>window.location.href = "login.php";</script>'; exit; } // Check if testimonial ID is provided if (isset($_GET['id'])) { $testimonial_id = $_GET['id']; // Fetch testimonial details $query = "SELECT * FROM testimonials WHERE testimonial_id = ?"; $stmt = mysqli_prepare($conn, $query); mysqli_stmt_bind_param($stmt, 'i', $testimonial_id); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); $testimonial = mysqli_fetch_assoc($result); if (!$testimonial) { echo '<script>alert("Testimonial not found!"); window.location.href = "testimonials.php";</script>'; exit; } } else { echo '<script>alert("Invalid request!"); window.location.href = "testimonials.php";</script>'; exit; } // Handle form submission if (isset($_POST['submit'])) { $name = $_POST['name']; $designation = $_POST['designation']; $comment = $_POST['comment']; $new_image = $_FILES['image']['name']; $image_folder = 'uploads/testimonials/'; $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif']; $max_file_size = 2 * 1024 * 1024; // 2 MB if (!empty($new_image)) { $image_tmp = $_FILES['image']['tmp_name']; $file_extension = strtolower(pathinfo($new_image, PATHINFO_EXTENSION)); $new_image_path = $image_folder . $new_image; // Validate file type and size if (!in_array($file_extension, $allowed_extensions)) { echo '<script>alert("Invalid file type! Only JPG, JPEG, PNG, and GIF are allowed.");</script>'; } elseif ($_FILES['image']['size'] > $max_file_size) { echo '<script>alert("File size exceeds 2 MB!");</script>'; } else { if (!is_dir($image_folder)) { mkdir($image_folder, 0777, true); } if (move_uploaded_file($image_tmp, $new_image_path)) { // Delete old image $old_image_path = $image_folder . $testimonial['image']; if (file_exists($old_image_path)) { unlink($old_image_path); } } else { echo '<script>alert("Failed to upload new image!");</script>'; } } } else { $new_image = $testimonial['image']; } // Update testimonial in database $update_query = "UPDATE testimonials SET name = ?, designation = ?, comment = ?, image = ? WHERE testimonial_id = ?"; $stmt = mysqli_prepare($conn, $update_query); mysqli_stmt_bind_param($stmt, 'ssssi', $name, $designation, $comment, $new_image, $testimonial_id); if (mysqli_stmt_execute($stmt)) { echo '<script>alert("Testimonial updated successfully!"); window.location.href = "testimonial.php";</script>'; } else { echo '<script>alert("Failed to update testimonial!");</script>'; } } ?> <!DOCTYPE html> <html lang="en"> <head> <?php include('style.php'); ?> <title>Edit Testimonial</title> <style> .edit-testimonial-container { max-width: 600px; margin: 0 auto; padding: 20px; background: #f9f9f9; border-radius: 8px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); } .edit-testimonial-container h1 { text-align: center; margin-bottom: 20px; } .edit-testimonial-container label { display: block; margin-bottom: 5px; font-weight: bold; } .edit-testimonial-container input, .edit-testimonial-container textarea { width: 100%; padding: 10px; margin-bottom: 15px; border: 1px solid #ccc; border-radius: 5px; } .edit-testimonial-container button { width: 100%; padding: 10px; background: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } .edit-testimonial-container button:hover { background: #0056b3; } .edit-testimonial-container img { max-width: 150px; margin-bottom: 10px; } </style> </head> <body> <?php include('sidebar.php'); ?> <div class="home-section"> <div class="edit-testimonial-container"> <h1>Edit Testimonial</h1> <form action="" method="POST" enctype="multipart/form-data"> <label for="name">Name</label> <input type="text" id="name" name="name" value="<?php echo htmlspecialchars($testimonial['name']); ?>" required> <label for="designation">Designation</label> <input type="text" id="designation" name="designation" value="<?php echo htmlspecialchars($testimonial['designation']); ?>" > <label for="comment">Comment</label> <textarea id="comment" name="comment" rows="5" required><?php echo htmlspecialchars($testimonial['comment']); ?></textarea> <label for="image">Image</label> <input type="file" id="image" name="image"> <p>Current Image:</p> <img src="uploads/testimonials/<?php echo htmlspecialchars($testimonial['image']); ?>" alt="Testimonial Image"> <button type="submit" name="submit">Update Testimonial</button> </form> </div> </div> <?php include('footer.php'); ?> </body> </html>