D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
mtfproduction.com
/
mtadminf
/
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; } // Function to handle image upload function image_upload($image_file, $existing_image_path = null) { if (isset($image_file) && $image_file['error'] == 0) { $target_dir = "uploads/"; $target_file = $target_dir . basename($image_file["name"]); $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); $check = getimagesize($image_file["tmp_name"]); if ($check !== false) { if (move_uploaded_file($image_file["tmp_name"], $target_file)) { return basename($image_file["name"]); } else { return $existing_image_path; } } else { return $existing_image_path; } } else { return $existing_image_path; } } // Check if form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Validate and sanitize form inputs $name = mysqli_real_escape_string($conn, $_POST['name']); $comment = mysqli_real_escape_string($conn, $_POST['comment']); $existing_image_path = $_POST['existing_image'] ?? null; // Handle image upload $imagePath = image_upload($_FILES['image'], $existing_image_path); // Insert or update testimonial details if (isset($_POST['add_testimonial'])) { $query = "INSERT INTO `testimonial` (`name`, `comment`, `image`) VALUES ('$name', '$comment', '$imagePath')"; } else if (isset($_POST['edit_testimonial'])) { $id = intval($_POST['id']); $query = "UPDATE `testimonial` SET `name`='$name', `comment`='$comment', `image`='$imagePath' WHERE `id`=$id"; } if (mysqli_query($conn, $query)) { header("Location: testimonial.php"); exit; } else { echo "Error updating testimonial: " . mysqli_error($conn); exit; } } // Fetch testimonial details for editing $testimonial = null; if (isset($_GET['id'])) { $id = intval($_GET['id']); $query = "SELECT * FROM `testimonial` WHERE `id`=$id"; $result = mysqli_query($conn, $query); if ($result && mysqli_num_rows($result) > 0) { $testimonial = mysqli_fetch_assoc($result); } else { echo "Testimonial not found!"; exit; } } ?> <!DOCTYPE html> <html lang="en"> <head> <?php require('style.php'); ?> </head> <body> <div class="page-wrapper default-wrapper" id="pageWrapper"> <div class="page-header"> <div class="header-wrapper row m-0"> <div class="header-logo-wrapper col-auto p-0"> <div class="logo-wrapper"><a href="index.php"><img class="img-fluid" src="assets/images/mtflogo.png" alt=""></a></div> </div> <div class="nav-right col-auto ms-auto"> <ul class="nav-menus"> <li class="profile-nav onhover-dropdown p-0"> <div class="d-flex align-items-center profile-media"><img class="b-r-10 img-40" src="assets/images/dashboard/profile.png" alt=""> <div class="flex-grow-1"><span>mtf </span> <p class="mb-0">Welcome Admin</p> </div> </div> </li> </ul> </div> </div> </div> <div class="page-body-wrapper default-menu"> <?php require('sidebar.php') ?> <div class="page-body"> <div class="container-fluid"> <div class="page-title"> <div class="row"> <div class="col-sm-6"> <h3><?php echo $testimonial ? "Update Testimonial" : "Create Testimonial"; ?></h3> </div> </div> </div> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST" enctype="multipart/form-data"> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <div class="card"> <div class="card-body"> <div class="form theme-form"> <div class="row"> <div class="col"> <div class="mb-3"> <label>Testimonial Name</label> <input class="form-control" type="text" name="name" placeholder="Testimonial Name *" value="<?php echo $testimonial ? htmlspecialchars($testimonial['name'], ENT_QUOTES, 'UTF-8') : ''; ?>"> </div> </div> </div> <div class="row"> <div class="col"> <div class="mb-3"> <label>Comment</label> <input class="form-control" type="text" name="comment" placeholder="Comment *" value="<?php echo $testimonial ? htmlspecialchars($testimonial['comment'], ENT_QUOTES, 'UTF-8') : ''; ?>"> </div> </div> </div> <div class="row"> <div class="col"> <div class="mb-3"> <label>Designation</label> <input class="form-control" type="text" name="designation" placeholder="designation *" value="<?php echo $testimonial ? htmlspecialchars($testimonial['designation'], ENT_QUOTES, 'UTF-8') : ''; ?>"> </div> </div> </div> <div class="row"> <div class="col"> <div class="mb-3"> <label>Upload Testimonial Image</label><br> <?php if ($testimonial) : ?> <img src="uploads/<?php echo htmlspecialchars($testimonial['image'], ENT_QUOTES, 'UTF-8'); ?>" alt="Testimonial Image" width="150px"><br> <input type="hidden" name="existing_image" value="<?php echo htmlspecialchars($testimonial['image'], ENT_QUOTES, 'UTF-8'); ?>"> <?php endif; ?> <input type="file" name="image" placeholder="Upload Image" class="form-control" accept=".jpg, .jpeg, .png, .svg"> </div> </div> </div> <div class="row"> <div class="col"> <button type="submit" class="btn btn-success" name="<?php echo $testimonial ? 'edit_testimonial' : 'add_testimonial'; ?>"><?php echo $testimonial ? 'Update Testimonial' : 'Create Testimonial'; ?></button> <button type="reset" class="btn btn-outline-secondary">Cancel</button> </div> </div> <?php if ($testimonial) : ?> <input type="hidden" name="id" value="<?php echo htmlspecialchars($testimonial['id'], ENT_QUOTES, 'UTF-8'); ?>"> <?php endif; ?> </div> </div> </div> </div> </div> </div> </form> </div> <?php require('footer.php') ?> </div> </div> </div> </body> </html>