i made a query to do my work. but it does that work with some errors.
1)i have 6 image fields and some other fields in the mysql table
2)to update i use html form with 6 file upload filed and labeled as image1 image2…
3)so if i want to update only the 5th image i have to upload new image to image5 filed in form.
to update mysql table i made this query.
$image=array(
1 =>$_FILES['image1']['name'],
2 =>$_FILES['image2']['name'],
3 =>$_FILES['image3']['name'],
4 =>$_FILES['image4']['name'],
5 =>$_FILES['image5']['name'],
6 =>$_FILES['image6']['name'],
);
$i=1;
$sql = "UPDATE salehotel ";
$sql .="SET"." ";
foreach($image as $value){
if(!$value==""){
$sql .= "`image".$i."`"."="."'". $value ."'".",". " ";
$i++;
}
}
$sql .="
`name`='$name',
`status`='$status',
`type`='$type',
`location`='$location',
`price`='$price',
`description`='$description'
WHERE
`property_id`='$edit'
";
3)when i use this to update all 6 images there is no errors and it updates every thing.
4)but when i try to update single image, (ex: think if i want to update image5) either i select a file from image5 field, this query always update first image in the table.
5)i know why it happens. in my query inside the foreach loop i’m looking only for $values not equal to “”. and then increment $i. so it incremented only one time. so it update image1 either i wanted to update image5.
6) so how can i get ride of this error ?
THANKS IN ADVANCE.
1 Answer