I have a seriously problem about updating image in the DB.The script below works fine if a user uploads an Image but if no image is uploaded then it updates the filename coloumn in the DB with empty space even though the image is still in the image directory.
What I want is to prevent the empty space and just leave the filename in the DB if no image is uploaded.
please look at my code below ,sorry that is too long
PHP Code:if (isset($_POST['btnSubmit'])){
// The agent's details have been updated.
$id=$_POST['id'];
$file_image=$_POST['file_image'];
$file_image2=$_POST['file_image2'];
$file_image3=$_POST['file_image3'];
$file_image4=$_POST['file_image4'];
$save_image=array();
while(list($key,$value) = each($_FILES['cons_image']['name']))
{
if(!empty($value)){ // this will check if any blank field is entered
$size=getimagesize($_FILES['cons_image']['tmp_name'][$key]);
$sizekb=filesize($_FILES['cons_image']['tmp_name'][$key]);
//reads the name of the file the user submitted for uploading
$image=$_FILES['cons_image']['name'][$key];
// if it is not empty
// get the original name of the file from the clients machine
$filename = stripslashes($_FILES['cons_image']['name'][$key]);
// get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
$rand= rand(0, 1000000000);
//we will give an unique name, for example a random number
$image_name=$rand.'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$consname="image/".$image_name; //change the image/ section to where you would like the original image to be stored
$consname2="image/thumb".$image_name; //change the image/thumb to where you would like to store the new created thumb nail of the image
$copied = copy($_FILES['cons_image']['tmp_name'][$key], $consname);
$copied = copy($_FILES['cons_image']['tmp_name'][$key], $consname2);
$save_image[] = $image_name;
//we verify if the image has been uploaded, and print error instead
if (!$copied) {
$error[] = '<p>Image was not uploaded!</p>';
}
else
{
// the new thumbnail image will be placed in images/thumbs/ folder
$thumb_name=$consname2 ;
// call the function that will create the thumbnail. The function will get as parameters
//the image name, the thumbnail name and the width and height desired for the thumbnail
$thumb=make_thumb($consname,$thumb_name,WIDTH,HEIGHT);
}
}
}
$thumb_path = "thumb".$file_image;
$thumb_path2 = "thumb".$file_image2;
$thumb_path3 = "thumb".$file_image3;
$thumb_path4 = "thumb".$file_image4;
if(!empty($save_image[0])){
@unlink("image/$file_image");
@unlink("image/$thumb_path");
}
if(!empty($save_image[1])){
@unlink("image/$file_image2");
@unlink("image/$thumb_path2");
}
if(!empty($save_image[2])){
@unlink("image/$file_image3");
@unlink("image/$thumb_path3");
}
if(!empty($save_image[3])){
@unlink("image/$file_image4");
@unlink("image/$thumb_path4");
}
if(!empty($save_image[0])){
$sql = "UPDATE property_list SET
image_filename='$save_image[0]'
WHERE prop_id='$id'";
$query = mysql_query($sql)or die(mysql_error());
}
if(!empty($save_image[1])){
$sql = "UPDATE property_list SET
image_filename2='$save_image[1]'
WHERE prop_id='$id'";
$query = mysql_query($sql)or die(mysql_error());
}
if(!empty($save_image[2])){
$sql = "UPDATE property_list SET
image_filename3='$save_image[2]'
WHERE prop_id='$id'";
$query = mysql_query($sql)or die(mysql_error());
}
if(!empty($save_image[3])){
$sql = "UPDATE property_list SET
image_filename4='$save_image[3]'
WHERE prop_id='$id'";
$query = mysql_query($sql)or die(mysql_error());
}
/*$sql = "UPDATE property_list SET
image_filename='$save_image[0]',
image_filename2='$save_image[1]',
image_filename3='$save_image[2]',
image_filename4='$save_image[3]'
WHERE prop_id='$id'";*/
if (@mysql_query($sql)) {
$display = '<p><strong>Image updated.</strong></p>';
} else {
$display = '<p>Image was not updated: ' .
mysql_error() . '</p>';
}
}
HTML Code:<label>Change Image 1</label> <input type='file' name='cons_image[]' class='bginput' /> </p> <p> <label>Change Image 2</label> <input type='file' name='cons_image[]' class='bginput' /> </p> <p> <label>Change Image 3</label> <input type='file' name='cons_image[]' class='bginput' /> </p> <p> <label>Change Image 4</label> <input type='file' name='cons_image[]' class='bginput' /> </p> <input type="hidden" name="file_image" value="<?php echo $filename;?>"/> <input type="hidden" name="file_image2" value="<?php echo $filename2;?>"/> <input type="hidden" name="file_image3" value="<?php echo $filename3;?>"/> <input type="hidden" name="file_image4" value="<?php echo $filename4;?>"/> <input type="hidden" name="id" value="<?php echo $id;?>"/> <input type="submit" value="Submit" name="btnSubmit"/>





Bookmarks