php mysql update form with image upload

Solutions on MaxInterview for php mysql update form with image upload by the best coders in the world

showing results for - "php mysql update form with image upload"
Forrest
03 Jun 2017
1<?php
2include "header.php";
3
4$banner_id = $_GET['id'];
5$query = "SELECT * FROM banners WHERE id = $banner_id";
6$q_result = mysqli_query($obj->conn, $query);
7$res = mysqli_fetch_assoc($q_result);
8
9
10if (isset($_POST['submit']))
11{
12    $title = $_POST['title'];
13    $des = $_POST['discription'];
14
15    $targetDir = "uploads/banners/";
16    $fileName = basename($_FILES['banner']['name']);
17    $targetFilePath = $targetDir . $fileName;
18    $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
19    move_uploaded_file($_FILES["banner"]["tmp_name"], $targetFilePath);
20
21    if (!empty($fileName))
22    {
23        $sql = "UPDATE `banners` SET `title`='$title',`data`='$des',`image`='$fileName' WHERE `id`= '$banner_id'";
24    }
25    else
26    {
27        $sql = "UPDATE `banners` SET `title`='$title',`data`='$des' WHERE `id`= '$banner_id'";
28    }
29
30    $result = mysqli_query($obj->conn, $sql);
31
32    if ($result == 'true')
33    {
34        echo "<script>alert('Banner updated successfully.')</script>";
35        echo "<script>window.location = 'banners.php';</script>";
36    }
37    else
38    {
39        echo "<script>alert('Error')</script>";
40    }
41}
42
43?>
44