Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if i change the image it store the values into data base. if won't give it does not save the edited record. Now I want to update record without change image and also i want to update records by only changing the image. my code as follow
PHP
<?php 
	if(isset($_POST['submit']))
	{
		$var=$_GET['a_id'];
		$file=$_SERVER['DOCUMENT_ROOT'].'myproject/cp/image/'.$img['a_image'];
		unlink($file);
		$var1=$_POST['Title'];
		$var2=$_POST['Description'];
		$image=$_FILES['imageupload']['name'];
		$root=$_SERVER['DOCUMENT_ROOT'].'/myproject/cp/image/'; 
		$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
		$expo=explode(".", $_FILES["imageupload"]["name"]); 
		$upload_exts = end($expo);
			if ((($_FILES["imageupload"]["type"] == "image/gif")|| ($_FILES["imageupload"]["type"] == "image/jpeg")|| ($_FILES["imageupload"]["type"] == "image/png")|| ($_FILES["imageupload"]["type"] == "image/jpg"))&& ($_FILES["imageupload"]["size"] < 2000000)&& in_array($upload_exts,$file_exts))
			{
					if ($_FILES["imageupload"]["error"] > 0)
					{
						echo "Return Code: " . $_FILES["imageupload"]["error"] . "<br>";
					}
					else
					{
						$image= rand(0,9999).'.'.$upload_exts;
						move_uploaded_file($_FILES["imageupload"]["tmp_name"],$root.$image);
						$sql=mysql_query("UPDATE about SET a_title='".$var1."',a_description='".$var2."',a_image='".$image."' WHERE a_id='".$var."'") or die(mysql_error());
						echo "<meta http-equiv=refresh content=\"0; URL=about.php\">";
					}
			}		
	}
?>
please help me to over come from this problem.
Posted
Updated 5-Sep-22 1:06am

1 solution

First get the previously saved image using the following

and then your code has to like this
PHP
<?php <br mode="hold" /?>	if(isset($_POST['submit']))
	{
		$var=$_GET['h_id'];
		$file=$_SERVER['DOCUMENT_ROOT'].'myproject/cp/image/'.$img['h_image'];
		$var1=$_POST['Title'];
		$var2=$_POST['Description'];
		$image=$_FILES['imageupload']['name'];
		$root=$_SERVER['DOCUMENT_ROOT'].'/myproject/cp/image/'; 
		$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
		$expo=explode(".", $_FILES["imageupload"]["name"]); 
		$upload_exts = end($expo);
			$image = '';
			if(isset($_FILES["imageupload"]['name']) && !empty($_FILES["imageupload"]['name']))//check image set or not.
			{
				if (!(($_FILES["imageupload"]["type"] == "image/gif")|| ($_FILES["imageupload"]["type"] == "image/jpeg")|| ($_FILES["imageupload"]["type"] == "image/png")|| ($_FILES["imageupload"]["type"] == "image/jpg")))
				{
					$error = "Invalid image";
				}else
				{
					$image= rand(0,9999).'.'.$upload_exts;
					move_uploaded_file($_FILES["imageupload"]["tmp_name"],$root.$image);	
				}
				unlink($file);//place unlink function here.
			}
			else
			{
				
				$image = $_POST['old_image'];
			}
				
			$sql=mysql_query("UPDATE home SET h_title='".$var1."',h_description='".$var2."',h_image='".$image."' WHERE h_id='".$var."'") or die(mysql_error());			

			echo "<meta http-equiv=refresh content=\"0; URL=home.php\">";
					
					
	}  
?>

thank u.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900