Php mysql update form with image upload
<?php include "header.php"; $banner_id = $_GET['id']; $query = "SELECT * FROM banners WHERE id = $banner_id"; $q_result = mysqli_query($obj->conn, $query); $res = mysqli_fetch_assoc($q_result); if (isset($_POST['submit'])) { $title = $_POST['title']; $des = $_POST['discription']; $targetDir = "uploads/banners/"; $fileName = basename($_FILES['banner']['name']); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); move_uploaded_file($_FILES["banner"]["tmp_name"], $targetFilePath); if (!empty($fileName)) { $sql = "UPDATE `banners` SET `title`='$title',`data`='$des',`image`='$fileName' WHERE `id`= '$banner_id'"; } else { $sql = "UPDATE `banners` SET `title`='$title',`data`='$des' WHERE `id`= '$banner_id'"; } $result = mysqli_query($obj->conn, $sql); if ($result == 'true') { echo "<script>alert('Banner updated successfully.')</script>"; echo "<script>window.location = 'banners.php';</script>"; } else { echo "<script>alert('Error')</script>"; } } ?>