Reputation: 77
I am doing a db update form and i have 3 input files for images
<div class="row d-flex justify-content-around">
<div class="loadImg col-3 m-1 overflow-hidden">
<input type="file" id="fileUpload1" name="fileUpload1" accept=".jpg, .jpeg, .png"/>
<div class="mt-2" id="showImg1">
<img src="<?= $house['img1'] ?>" width="200" height="150"/>
</div>
</div>
<div class="loadImg col-3 m-1 overflow-hidden">
<input type="file" id="fileUpload2" name="fileUpload2" accept=".jpg, .jpeg, .png"/>
<div class="mt-2" id="showImg2">
<img src="<?= $house['img2'] ?>" width="200" height="150"/>
</div>
</div>
<div class="loadImg col-3 m-1 overflow-hidden">
<input type="file" id="fileUpload3" name="fileUpload3" accept=".jpg, .jpeg, .png"/>
<div class="mt-2" id="showImg3">
<img src="<?= $house['img3'] ?>" width="200" height="150"/>
</div>
</div>
</div>
the showImg div get the old img on the database or show a preview for the new one with js... My question is if I change only one or two files how to keep the old one unchanged? because when I post my form the unchanged img get the path 'public/img/' !!! I am sure that you guys have the answer ;)
THX
my update php request
public function modifyMyHouse()
{
$conn = $this->connect();
if (isset($_POST['modifyHouse'])) {
$author_id = $_SESSION['id'];
$zipCode = htmlspecialchars($_POST['houseZipCode']);
$city = htmlspecialchars($_POST['houseCity']);
$region = htmlspecialchars($_POST['houseRegion']);
$beds = htmlspecialchars($_POST['beds']);
$dateFrom = htmlspecialchars($_POST['dateFrom']);
$dateTo = htmlspecialchars($_POST['dateTo']);
$equipment = htmlspecialchars($_POST['equipment']);
$activity = htmlspecialchars($_POST['activity']);
$descriptive = htmlspecialchars($_POST['descriptive']);
$img1 = $_FILES['fileUpload1']['name'];
$img2 = $_FILES['fileUpload2']['name'];
$img3 = $_FILES['fileUpload3']['name'];
$update = $conn->prepare("UPDATE myHousePost SET author_id = :author_id, zip_code = :zipCode, house_city = :city, house_region = :region, beds = :beds, date_from = :dateFrom, date_to = :dateTo, equipment = :equipment, activity = :activity, descriptive = :descriptive, img1 = :img1, img2 = :img2, img3 = :img3, post_date = Now()");
$update->execute([':author_id'=>$author_id, ':zipCode'=>$zipCode, ':city'=>$city, ':region'=>$region, ':beds'=>$beds, ':dateFrom'=>$dateFrom, ':dateTo'=>$dateTo, ':equipment'=>$equipment, ':activity'=>$activity, ':descriptive'=>$descriptive, ':img1'=>'public/img/' . $img1 , ':img2'=>'public/img/' . $img2 , ':img3'=>'public/img/' . $img3]);
if (!empty($update)) {
move_uploaded_file($_FILES['fileUpload3']['tmp_name'], 'public/img/' . $img1);
move_uploaded_file($_FILES['fileUpload3']['tmp_name'], 'public/img/' . $img2);
move_uploaded_file($_FILES['fileUpload3']['tmp_name'], 'public/img/' . $img3);
header('location:/home-switch-home/mon-annonce');
} else{
header('location:/home-switch-home/404');
}
}
}
Upvotes: 2
Views: 65
Reputation: 77
UPDATE with answer
I just update Ronak Dhoot code!!!
public function modifyMyHouse()
{
$conn = $this->connect();
if (isset($_POST['modifyHouse'])) {
$author_id = $_SESSION['id'];
$zipCode = htmlspecialchars($_POST['houseZipCode']);
$city = htmlspecialchars($_POST['houseCity']);
$region = htmlspecialchars($_POST['houseRegion']);
$beds = htmlspecialchars($_POST['beds']);
$dateFrom = htmlspecialchars($_POST['dateFrom']);
$dateTo = htmlspecialchars($_POST['dateTo']);
$equipment = htmlspecialchars($_POST['equipment']);
$activity = htmlspecialchars($_POST['activity']);
$descriptive = htmlspecialchars($_POST['descriptive']);
$img1 = $_FILES['fileUpload1']['name'];
$img2 = $_FILES['fileUpload2']['name'];
$img3 = $_FILES['fileUpload3']['name'];
$updateSql = ("UPDATE myHousePost SET author_id = :author_id, zip_code = :zipCode, house_city = :city, house_region = :region, beds = :beds, date_from = :dateFrom, date_to = :dateTo, equipment = :equipment, activity = :activity, descriptive = :descriptive");
$updateValues = ([':author_id'=>$author_id, ':zipCode'=>$zipCode, ':city'=>$city, ':region'=>$region, ':beds'=>$beds, ':dateFrom'=>$dateFrom, ':dateTo'=>$dateTo, ':equipment'=>$equipment, ':activity'=>$activity, ':descriptive'=>$descriptive]);
if($_FILES['fileUpload1']['error'] !== 4) {
$updateSql .= ",img1 = :img1 ";
$updateValues[':img1'] = 'public/img/'. $img1;
}
if($_FILES['fileUpload2']['error'] !== 4) {
$updateSql .= ",img2 = :img2 ";
$updateValues[':img2'] = 'public/img/'. $img2;
}
if($_FILES['fileUpload3']['error'] !== 4) {
$updateSql .= ",img3 = :img3 ";
$updateValues[':img3'] = 'public/img/'. $img3;
}
$updateSql .= ",post_date = Now()";
$update = $conn->prepare($updateSql);
$update->execute($updateValues);
if (!empty($update)) {
move_uploaded_file($_FILES['fileUpload1']['tmp_name'], 'public/img/' . $img1);
move_uploaded_file($_FILES['fileUpload2']['tmp_name'], 'public/img/' . $img2);
move_uploaded_file($_FILES['fileUpload3']['tmp_name'], 'public/img/' . $img3);
header('location:/home-switch-home/mon-annonce');
} else{
header('location:/home-switch-home/404');
}
}
}
Upvotes: 0
Reputation: 2321
As it is untouched $_FILES['fileUpload1']['name'] will be null
<?php
$img1 = $_FILES['fileUpload1']['name'];
$img2 = $_FILES['fileUpload2']['name'];
$img3 = $_FILES['fileUpload3']['name'];
$sql = "UPDATE myHousePost SET author_id = :author_id, zip_code = :zipCode, house_city = :city, house_region = :region, beds = :beds, date_from = :dateFrom, date_to = :dateTo, equipment = :equipment, activity = :activity, descriptive = :descriptive,";
$values = [':author_id'=>$author_id, ':zipCode'=>$zipCode, ':city'=>$city, ':region'=>$region, ':beds'=>$beds, ':dateFrom'=>$dateFrom, ':dateTo'=>$dateTo, ':equipment'=>$equipment, ':activity'=>$activity, ':descriptive'=>$descriptive, ':img1'=>'public/img/' . $img1 , ':img2'=>'public/img/' . $img2 , ];
if($img1 !== null) {
$sql .= "img1 = :img1, ";
$values[':img1'] = 'public/img/'. $img1;
}
if($img2 !== null) {
$sql .= "img2 = :img2, ";
$values[':img2'] = 'public/img/'. $img2;
}
if($img3 !== null) {
$sql .= "img3 = :img3, ";
$values[':img3'] = 'public/img/'. $img3;
}
$sql .= "post_date = Now()";
$update = $conn->prepare($sql);
$update->execute($values);
if (!empty($update)) {
move_uploaded_file($_FILES['fileUpload3']['tmp_name'], 'public/img/' . $img1);
move_uploaded_file($_FILES['fileUpload3']['tmp_name'], 'public/img/' . $img2);
move_uploaded_file($_FILES['fileUpload3']['tmp_name'], 'public/img/' . $img3);
header('location:/home-switch-home/mon-annonce');
} else{
header('location:/home-switch-home/404');
}
Upvotes: 3