I’m trying to update the supplier details along with the image. The details are being updated to the database but the image is not getting updated – the old pic is still on the database. No error is shown as well. The code looks fine to me. pls have a look and help me out. I’m also including the supplier edit page code for better understanding. This same code works perfectly for my other page but on this page, it’s not uploading the new image to the file and also the database.
Code.php:
if(isset($_POST['sup_updatebtn'])){ $sup_id=$_POST['sup_update_id']; $sup_name=$_POST['sup_edit_name']; $sup_regno=$_POST['sup_edit_regno']; $sup_address=$_POST['sup_edit_address']; $sup_phone=$_POST['sup_edit_phone']; $sup_email=$_POST['sup_edit_email']; $sup_img_old=$_POST['sup_doc_old']; $imgFile = $_FILES['sup_doc']['name']; $tmp_dir = $_FILES['sup_doc']['tmp_name']; $imgSize = $_FILES['sup_doc']['size']; if($imgFile) { $upload_dir = 'sup_upload/'; // upload directory $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension $valid_extensions = array('pdf','jpeg', 'jpg', 'png', 'gif'); // valid extensions if(in_array($imgExt, $valid_extensions)) { if(!file_exists("sup_upload/".$imgFile)) { if($imgSize < 5000000) { unlink($upload_dir.$sup_img_old); move_uploaded_file($tmp_dir,$upload_dir.$imgFile); } else { $_SESSION['status']="Sorry, your file is too large it should be less then 5MB"; header("Location:supplier.php"); } } else { $filename=$_FILES['sup_doc']['name']; $_SESSION['status']= "Sorry, existing file: $filename"; header("Location:supplier.php"); } } else { $_SESSION['status']= "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; header("Location:supplier.php"); } } else { // if no image selected the old image remain as it is. $imgFile = $sup_img_old; // old image from database } if(!isset($_SESSION['status'])) { $query=$conn->prepare("UPDATE supplier SET sup_name=?, sup_regno=?, sup_address=?,sup_phone=?,sup_email=?,sup_doc=? WHERE sup_id=?"); $query->bind_param("sssissi",$sup_name,$sup_regno,$sup_address,$sup_phone,$sup_email,$imgFile,$sup_id); $query->execute(); if ($query->affected_rows>0) { $_SESSION['success']="Your Data is Updated"; header("Location:supplier.php"); } else { $_SESSION['status']="Your Data is NOT Updated"; header("Location:supplier.php"); } } }
Supplier_edit.php
<div class="modal-body"> <?php //DISPLAY or RETRIEVE DATA TO EDIT if(isset($_POST['sup_edit_btn'])) { $sup_edit_id= $_POST['sup_edit_id']; $query="SELECT * FROM supplier WHERE sup_id='$sup_edit_id'"; $query_run=mysqli_query($conn,$query); foreach($query_run as $row) { ?> <form action="code.php" method="POST"> <input type="hidden" name="sup_update_id" value="<?php echo $row['sup_id']?>"> <div class="form-group"> <label>Company Name</label> <input type="text" name="sup_edit_name" value="<?php echo $row['sup_name']?>" class="form-control"> </div> <div class="form-group"> <label>Registration Number</label> <input type="text" name="sup_edit_regno" value="<?php echo $row['sup_regno']?>" class="form-control" > </div> <div class="form-group"> <label>Address</label> <input type="text" name="sup_edit_address" value="<?php echo $row['sup_address']?>" class="form-control" > </div> <div class="form-group"> <label>Phone Number</label> <input type="tel" name="sup_edit_phone" value="<?php echo $row['sup_phone']?>" class="form-control" > </div> <div class="form-group"> <label>Email Address</label> <input type="email" name="sup_edit_email" value="<?php echo $row['sup_email']?>" class="form-control" > </div> <label>Supplier Document</label> <input type="file" name="sup_doc" id="sup_doc" class="form-control"/> <input type="hidden" name="sup_doc_old" value="<?php echo $row['sup_doc'];?>"/> </div> <!-- Display image --> <img src="<?php echo "sup_upload/".$row['sup_doc'];?>" height = 90px width = 90px > </br> </div> <button type="submit" name="sup_updatebtn" class="btn btn-primary">Update</button> <a href="supplier.php" class="btn btn-danger">Cancel</a> </form> <?php }}?> </div>
Advertisement
Answer
add enctype=”multipart/form-data” to your form
<form action="code.php" method="POST" enctype="multipart/form-data">