无法更新图像,但可以更新详细信息



我正在尝试更新供应商的详细信息以及图片。详细信息正在更新到数据库中,但图像没有更新——旧照片仍在数据库中。也没有显示错误。代码对我来说很好。请看一看并帮助我。我还包括供应商编辑页面代码,以便更好地理解。同样的代码非常适用于我的另一个页面,但在这个页面上,它没有将新图像上传到文件和数据库中。

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>

add enctype="多部分/形式数据";到您的表单

<form action="code.php" method="POST" enctype="multipart/form-data">

相关内容

最新更新