Having a problem with updating a picture.I need to replace the current picture in the DB by allowing the users to browse a picture and uploading it.And if the user did not upload any image I need to keep the current picture as it is.

I wrote something like this,but its not working ..

if (isset($_POST['btnModifyCategory'])){
	
	$cid         = $_POST['catId'];
	$name2        = $_POST['txtName'];
	$description2 = $_POST['catDesc'];

	if(isset($_FILES['file']))
	{
			$image2 = uploadImage();	
	}
	 	
	//update the database with new values.
	if(isset($_FILES['file']))
		{
			if($_FILES['file']['name'] != "")
			{
				$sql2    = "UPDATE tbl_category SET cat_name ='$name2', cat_description='$description',cat_image='$image' WHERE cat_id = '$cid'";
				$result2 = mysql_query($sql2) or die(mysql_error());
			}
			else
			{
				$sql2    = "UPDATE tbl_category SET cat_name ='$name2',cat_description='$description' WHERE cat_id = '$cid'";
				$result2 = mysql_query($sql2) or die(mysql_error());
			
			}
	}
}

(Im not posting the uploadImage() here ,since it is working properly.)
How can I do it..Can someone do it for me.Thank you..!

Dani AI

Generated

A few quick, practical points to fix this reliably.

The real goal is: only change the DB image column when a new file was actually uploaded and successfully saved. Check the upload status (not just isset($_FILES['file'])), call your uploadImage() and use its returned filename only when the upload succeeded, and make sure the variable names you use are consistent. already flagged the variable-name mismatch; that kind of typo will break the logic. 's suggestion to echo/log intermediate values is also useful while debugging.

Example workflow (using PDO and prepared statements — safer than old mysql_*):

// assume $pdo is a PDO instance with ERRMODE_EXCEPTION
$cid  = $_POST['catId'];
$name = $_POST['txtName'];
$desc = $_POST['catDesc'];

$newFilename = null;
if (!empty($_FILES['file']['tmp_name'])
    && is_uploaded_file($_FILES['file']['tmp_name'])
    && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
    // uploadImage should move the file and return its stored filename or false
    $newFilename = uploadImage($_FILES['file']);
}

if ($newFilename) {
    $sql = "UPDATE tbl_category SET cat_name = :name, cat_description = :desc, cat_image = :img WHERE cat_id = :id";
    $params = [':name'=>$name, ':desc'=>$desc, ':img'=>$newFilename, ':id'=>$cid];
} else {
    $sql = "UPDATE tbl_category SET cat_name = :name, cat_description = :desc WHERE cat_id = :id";
    $params = [':name'=>$name, ':desc'=>$desc, ':id'=>$cid];
}
$pdo->prepare($sql)->execute($params);

Troubleshooting and safety notes:

  • Check $_FILES['file']['error'] and is_uploaded_file() instead of only isset().
  • If you move the new file but the DB update fails, delete the moved file to avoid orphan files. Conversely, delete the old file only after a successful update.
  • Validate file type with finfo_file(), restrict size, and never trust original filenames (generate a safe unique name).
  • Use prepared statements to avoid SQL injection and set PDO to throw exceptions for easier debugging.

For PHP upload reference see the official manual: PHP file upload handling.

Recommended Answers

All 3 Replies

Isnt there anyone who can help me with this issue?? any expert?? Please..:(

Define not working. Have you done any debugging and isolated where it is having a problem? You presumably have a test environment where you can try this. We don't. When I started working on computers we had to desk check everything because there was no choice. Now we can let the computer do some of the work for us. When something doesn't work, you debug it and try to determine what isn't working the way you expected. There are some better ways but the simple echo command can be used to give you more info on the value of variables at different points in the program so you can see if it doing exactly what you expected it would. If the database reads or writes aren't working properly you can echo the select / insert / update command and then try it in phpMyAdmin to see what it returns.

Firstly I don't understand why in one place you set $description2 but everywhere use $description? The same with $image. Check the names, are they really correct?

Don't know what your function uploadImage() returns, but in the database you need to store only the picture name. Take a look at this article if you have problems with uploading.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.