Image Upload Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Image Upload Problem

Post by nitediver »

The problem is, if the input field for image I left empty(because it's already has image in database), it keep processing the "empty input field", overwrite the current, so deleting the current image in database.

I already put checker for image existence using "mysql_num_rows", but nothing change.

How to prevent the empty input image field, from being process?

Code: Select all

if(isset($_POST['submit'])){
$prod_name = $_POST['prod_name'];
$prod_price = $_POST['prod_price'];
$prod_vendor = $_POST['prod_vendor'];
$prod_img = $_POST['prod_img'];
$prod_desc = $_POST['prod_desc'];
$prod_kat= htmlentities($_POST['prod_cat'], ENT_QUOTES);

//Image checker
$img_chk = mysql_num_rows(mysql_query("SELECT prod_img FROM a_prod WHERE prod_id='$prod_id'")); 
if($img_chk!=0){
//...
$prod_img = "img/".$_FILES['prod_img']['name'];
$name = $_FILES['img']['name'];
	if($prod_img != none)
	{
		if(copy($_FILES['prod_img']['tmp_name'],$prod_img))
		{
			echo "ok";
		}else{
			echo "bad | ";
		}
	}
}

$qupdate = mysql_query("UPDATE a_prod SET prod_name='$prod_name',prod_price='$prod_price',prod_vendor='$prod_vendor',prod_cat='$prod_cat',prod_img='$prod_img',prod_desc='$prod_desc' WHERE prod_id='$prod_id'");
	if($qupdate){
	echo "Product Update Success";
	}else{
	echo "Product Update Error";
	}
}
rnoack
Forum Commoner
Posts: 34
Joined: Mon May 03, 2010 12:38 am

Re: Image Upload Problem

Post by rnoack »

use an if statement around the code you don't want to run which checks if the image field is blank or not
Post Reply