Image path wont show in input text area

I am able to upload/delete the details in an admin form, but when I come to edit the category in admin, the path for the image doesnt show in the text field.

Ive tried all morning and cant work it out.

The code is below, if somebody wouldnt mind taking a look.


// ERROR FIELDS FOR THREE FIELDS IF BLANK
$Err1="";
$Err2="";
$Err3="";
$conf ="";
 // FLAG FOR SWITCHING FROM INSERTING OR UPDATING DATA
$flag=1;

 // DATA VARIABLES TO SET UP DATA IN PARTICULAR FIELDS
$name="";
$desc="";
$path="";

// IF CLICK DATA FOR EDITING FOLLOWING PROCEDURE FIRES TO FILL DATA
if(isset($_GET['cat_Id']))
{
$cat_Id=$_GET['cat_Id'];
$q=mysql_query("select * from category where cat_Id=$cat_Id") or die (mysql_error());	
$rows=mysql_fetch_assoc($q);
$name=$rows["category_Name"];
$meta=$rows["category_metaTitle"];
$path=$rows["category_Image"];
$desc=$rows["category_Description"];
$flag=0;
}

// PROCEDURE FOR DELETING DATA
if(isset($_POST['btndelete']))
{
$sr=$_POST['srno1'];
$flag=$_POST['flag'];
if ($flag==0)
{
		
$q23=mysql_query("delete from category where cat_Id=$sr") or die (mysql_error());
$flag=1;
}
else
{
		
}

}

//PROCEDIURE FOR INSERTING DATA
if(isset($_POST['btnsubmit']))
{
$name12=$_FILES['logo']['name'];
if($_POST['txtname']=="" || $_POST['txtdesc']=="") // || $name12=="")
{
	
// IF NAME FIELD BLANK SHOW ERROR BY ERR1 VAR
if($_POST['txtname']=="")
{
$Err1="blank field";
$desc=$_POST['txtdesc'];
$path=$name12;
}
// IF DESCRIPTION FIELD BLANK SHOW ERROR BY ERR2 VAR		
if($_POST['txtdesc']=="")
{
$Err2="blank field";
$name=$_POST['txtname'];
$path=$name12;
}
// IF FILE FIELD BLANK SHOW ERROR BY ERR3 VAR				
if($name12=="")
{
$Err3="blank field";
$name=$_POST['txtname'];
$desc=$_POST['txtdesc'];
}
}
else
{
// PROCEDURE FOR UPDATING DATA
$flag=$_POST['flag'];
if ($flag==0)
{

// CODE FOR GETTING IMAGE DETAILS 		
$name=$_POST['txtname'];
$desc1=$_POST['txtdesc'];	
$src=$_FILES['logo']['tmp_name'];
$fname=$_FILES['logo']['name'];
	
$sr=$_POST['srno1'];
$name=$_POST['txtname'];
$desc=$_POST['txtdesc'];

// CODE FOR UPDATING IMAGE IN /IMGDATA/CHAIN/<IMAGENAME> FOLDER 					
copy("$src","imgdata/category/$fname");
$q24=mysql_query("update category set category_Name='$name', category_Description='$desc1', category_Image='imgdata/category/$fname' where cat_Id=$sr") or die (mysql_error());
$flag=1;
$conf="Data Updated Successfully ";
}
else
{
$name=$_POST['txtname'];
$desc1=$_POST['txtdesc'];	
$src=$_FILES['logo']['tmp_name'];
$fname=$_FILES['logo']['name'];

			
//copy("$src",".//imgdata\\\\category\\\\$fname");
// CODE FOR INSERTING IMAGE IN /IMGDATA/CATEGORY/<IMAGENAME> FOLDER 								
copy("$src","imgdata/category/$fname");
$q=mysql_query("insert into category (category_Name, category_Description, category_Image) values('$name','$desc1','imgdata/category/$fname')") or die (mysql_error());	
$conf="Data Inserted Successfully";			
}
}
}

?>



<td bgcolor="#666666" width="50%"><input type="file" name="logo" value="<? echo '$path';?>" /></td>

<td width="44%"><input type="submit" name="btnsubmit" value="Submit"></td>
<td width="56%">
<div align="right">								
<input type="submit" name="btndelete" value="Delete" onClick="return check();">
<input type="hidden" name="srno1" value="<?= $rows["cat_Id"];?>">

I can send the log in details to the admin area if needs be.

Thank you

If I change the input type to text instead of file and change $path=$rows[“category_name”]; it seems to work.

But I’m still a bit stumped.

The problem is too, is because the input value is “”, it will clear any image in the database with nothing unless I re-upload the image with any change made again.

The rest of the fields are filled fine with the following code, its just the image bit with a file field type instead of text


if(isset($_GET['cat_Id']))
{
	$cat_Id=$_GET['cat_Id'];
	$q=mysql_query("select * from category where cat_Id=$cat_Id") or die (mysql_error());	
	$rows=mysql_fetch_assoc($q);
	$name=$rows["category_Name"];
	$meta=$rows["category_metaTitle"];
	$path=$rows["category_Image"];
	$desc=$rows["category_Description"];
	$flag=0;
}

The cuplrit is $path=$rows[“category_Image”];

I’m sorry i keep posting, but I’m just about all out now as I have tried everything.

The clock uploads fine to the database, the clock itself appears in the correct folder on the server, and the clock name with its path is correct in the database.

Why it doesnt show its path and name in the input field i dont know, and so it creates a problem where if the client goes in to edit anything and doesnt browse to the clock again, the clock name dissapears from the database but its path to the folder survives.

Ta

Though the way you are trying to do is not the way what it should be done in case of file upload things but your problem must be because you have printed the variable within a single quotes. So do as follows and it should print the value:


<input type="file" name="logo" value="<?php echo $path; ?>" />

But printing a value in file field is forbidden due to security issues. So while editing the image should be something like this:

  • Store the old file path in an hidden field.
  • Display the image in <IMG> tag as preview.
  • Have another empty/new file browse field.

On form submit,
If new file is uploaded then

  • Delete old image
  • Upload new one
  • Update database table with new file path/file name.

If file is not uploaded, then ignore the table update.

Hope you understand what I mean !

Hi Raju,

Good hear from you again.

Unfortunately that change didnt work, and I think I understood your reply, but putting it into practice might be a bit above me at the moment.

Basically I have inherited this system, and I’m trying to put my own stamp on it, but little set backs like this are really holding me back.

Thanks for replying.

If it would help you to see the code, I would be happy to give you FTP details, and access to the admin area.

Its such an annoying thing that I am given a site to work with that a client has used for so long, and then find that it wasnt working for them in the first place.

I couldn’t move on from this bit with resolving it first, as having to browse to the image everytime an update is needed, is no good as you would know.

Thanks

Looks like maybe I will need to start again with this, rather than re-hashing another persons work.

So can somebody point me in the right direction to a tutorial that will allow me to add, edit, delete information and also have an image uploader in there, and if possible as i will need it on another bit, the option to upload multiple images.

Could somebody please go through my code and show me how to sort the image upload and display problem.

Or if I somebody has a all in one add/edit/delete form with an image uploader that I can work with.

I think all I need is when i want to edit a category, to be able to display the image that already exists in the database, and then be able to change it if needed, but if not leave it as it is and the image stays in the database.

I know its probably very easy, but I am learning, and just cant get my head around it, and I have tried over the last couple of days.


<?php
// THIS PAGE BELONGS TO CHAIN MANAGEMENT

// FOLLOWING FILE USED TO SET UP DB CONNECTIVITY
include "config.php";
include "header.php";
// ERROR FIELDS FOR THREE FIELDS IF BLANK
//$Err1="";
//$Err2="";
//$Err3="";

 // FLAG FOR SWITCHING FROM INSERTING OR UPDATING DATA
$flag=1;

 // DATA VARIABLES TO SET UP DATA IN PARTICULAR FIELDS
$name="";
$meta="";
$desc="";
$path="";
$conf ="";

// IF CLICK DATA FOR EDITING FOLLOWING PROCEDURE FIRES TO FILL DATA
if(isset($_GET['cat_Id']))
{
	$cat_Id=$_GET['cat_Id'];
	$q=mysql_query("select * from category where cat_Id=$cat_Id") or die (mysql_error());	
	$rows=mysql_fetch_assoc($q);
	$name=$rows["category_Name"];
	$meta=$rows["category_metaTitle"];
	$path=$rows["category_Image"];
	$desc=$rows["category_Description"];
	$flag=0;
}

// PROCEDURE FOR DELETING DATA
if(isset($_POST['btndelete']))
{
	$sr=$_POST['srno1'];
	$flag=$_POST['flag'];
	if ($flag==0)
	{
		
		$q23=mysql_query("delete from category where cat_Id=$sr") or die (mysql_error());
		$flag=1;
	}
	else
	{
		
	}

}

//PROCEDIURE FOR INSERTING DATA
if(isset($_POST['btnsubmit']))
{
//$name12=$_FILES['logo']['name'];
	//if($_POST['txtname']=="" || $_POST['txtdesc']=="") // || $name12=="")
	//{
	
	//// IF NAME FIELD BLANK SHOW ERROR BY ERR1 VAR
		//if($_POST['txtname']=="")
		//{
			//$Err1="blank field";
			//$desc=$_POST['txtdesc'];
			//$path=$name12;
		//}
	//// IF DESCRIPTION FIELD BLANK SHOW ERROR BY ERR2 VAR		
		//if($_POST['txtdesc']=="")
		//{
			//$Err2="blank field";
			//$name=$_POST['txtname'];
			//$path=$name12;
		//}
	//// IF FILE FIELD BLANK SHOW ERROR BY ERR3 VAR				
		//if($name12=="")
		//{
			//$Err3="blank field";
			//$name=$_POST['txtname'];
			//$desc=$_POST['txtdesc'];
		//}
	//}
	//else
	//{
	// PROCEDURE FOR UPDATING DATA
		$flag=$_POST['flag'];
		if ($flag==0)
		{

		// CODE FOR GETTING IMAGE DETAILS 		
		$name=$_POST['txtname'];
		$desc1=$_POST['txtdesc'];	
		$src=$_FILES['logo']['tmp_name'];
		$fname=$_FILES['logo']['name'];
	
		$sr=$_POST['srno1'];
		$name=$_POST['txtname'];
		$desc=$_POST['txtdesc'];

		// CODE FOR UPDATING IMAGE IN /IMGDATA/CHAIN/<IMAGENAME> FOLDER 					
			copy("$src","imgdata/category/$fname");
			$q24=mysql_query("update category set category_Name='$name', category_Image='imgdata/category/$fname', category_Description='$desc1' where cat_Id=$sr") or die (mysql_error());
			$flag=1;
			$conf="Data Updated Successfully ";
		}
		else
		{
			$name=$_POST['txtname'];
			$desc1=$_POST['txtdesc'];	
			$src=$_FILES['logo']['tmp_name'];
			$fname=$_FILES['logo']['name'];

			
			//copy("$src",".//imgdata\\\\category\\\\$fname");
		// CODE FOR INSERTING IMAGE IN /IMGDATA/CATEGORY/<IMAGENAME> FOLDER 								
			copy("$src","imgdata/category/$fname");
			$q=mysql_query("insert into category (category_Name, category_Image, category_Description) values('$name','imgdata/category/$fname','$desc1')") or die (mysql_error());	
			$conf="Data Inserted Successfully";			
		}
	}
//}

?>
<script language="javascript" type="text/javascript">
function check()
{
	  flg=confirm("Data would be permanently deleted, Are you sure?");
	  if(flg==true)
 		{
		   	return true;	     	
			alert("Record Deleted");
		}	
  		else
   		{
			return false;
		}
}
</script>

<html>
<head>
<title>Sovereign Antiques Category Management</title>
<style type="text/css">
<!--
.f1 {
	font-family: tahoma;
	font-size: 13px;
	font-style: normal;
	line-height: normal;
	font-weight: bold;
	font-variant: normal;
	text-transform: none;
	color: #006600;
}
-->
</style>
</head>
<body link="555555" vlink="555555">
<form name="form1" action="admin_categories.php?index=<?=$index?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="flag" value="<?= $flag?>">
<table align="center">
<tr class="f1">
<td class="f1"><?= $conf ?></td>
</tr>
</table>
<table align="center" width="740" bordercolor="#00CCFF" bordercolordark="#0099FF" border="0" cellpadding="5" cellspacing="2">

<tr>
<td colspan="2" bgcolor="#222222" height="45" style="background-image:url(images/admin_Grey_Bar.jpg)">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:13px; color:#ffffff; font-weight:bold; position:relative; float:left; left:20px; top:-2px;">
Create/Amend a Category</div>
</td>
</tr>

<tr>
<td bgcolor="#666666" width="50%">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#ffffff; font-weight:bold; position:relative; float:left; left:10px;">Category Name:</div>
</td>				
<td bgcolor="#666666" width="50%"><input type="text" name="txtname" size="50" value="<? echo $name;?>" /></td>
</tr>

<tr>
<td bgcolor="#666666" width="50%">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#ffffff; font-weight:bold; position:relative; float:left; left:10px;">Category Description:</div>
</td>
<td bgcolor="#666666" width="50%"><input type="text" name="txtdesc" size="85" value="<? echo $desc;?>" style="height:70px;" /></td>
</tr>
				
<tr>
<td bgcolor="#666666" width="50%">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#ffffff; font-weight:bold; position:relative; float:left; left:10px;">Category Image:</div>
</td>
<td bgcolor="#666666" width="50%"><input type="file" name="logo" size="50" value="<? echo $path;?>" /></td>
</tr>

<tr>
<td bgcolor="#666666" width="50%"><br></td>
<td bgcolor="#666666" width="50%">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="44%"><input type="submit" name="btnsubmit" value="Submit"></td>
<td width="56%">
<div align="right">								
<input type="submit" name="btndelete" value="Delete" onClick="return check();">
<!-- SETTING HIDDEN FIELD FOR PASSING SRNO DATA TO NEXT PAGE-->
<input type="hidden" name="srno1" value="<?= $rows["cat_Id"];?>">
</div>
</td>
</tr>
</table>
</td>
</tr>

<tr>
<td>&nbsp;</td>
</tr>

<tr>
<td valign="top" height="45" bgcolor="#eeeeee" width="50%" style="background-image:url(images/admin_Grey_Bar_Smallest.jpg)">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:13px; color:#ffffff; font-weight:bold; position:relative; float:left; left:20px; top:7px;">Existing Categories</div>
</td>
<td bgcolor="#eeeeee" style="background-image:url(images/admin_Grey_Bar_509_No_Arrow.jpg)"></td>
</tr>	

<tr>
<td width="50%" bgcolor="#666666">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#ffffff; font-weight:bold; position:relative; float:left; left:10px;">Category Name</div></td>
<td width="300" bgcolor="#666666">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#ffffff; font-weight:bold; position:relative; float:left; left:10px;">Category Description</div></td>
</tr>

<!-- PHP CODE FOR FILL THE DATA FOR GRID TO EDIT OR DELETE-->				
<?php

				$q=mysql_query("select * from category ") or die (mysql_error());				
				while($row=mysql_fetch_assoc($q))
				{
					echo "<tr>";
					echo "<td bgcolor='#eeeeee' width='50%' valign='top'>";
						echo "<p style='position:relative; left:9px;'><font size='2' color='#555555' face='Verdana,Arial,Helvetica,sans-serif'><a href='admin_categories.php?cat_Id=".$row["cat_Id"]."&index=".$index."'>".$row["category_Name"]."</a></font></p>";
					echo "</td>";
					echo "<td bgcolor='#eeeeee' width='500' valign='top'><font size='2' color='#555555' face='Verdana,Arial,Helvetica,sans-serif' style='position:relative; left:9px;'>".$row["category_Description"]."</font></td>";
					echo "</tr>";

				}				

				?>
				
			</table>
</form>
		</div>
</body>
</html>

OK Im having a go at this after trying to work it out in my head.

I created a second arew to display the image path if edit is chosen, as I’m not allowed to display the image path in a file value field as below:

<?
if(isset($_GET[‘cat_Id’]))
{ ?>
<tr>
<td bgcolor=“#666666” width=“50%”>
<div style=“font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#ffffff; font-weight:bold; position:relative; float:left; left:10px;”>Current Category Image:</div>
</td>
<td bgcolor=“#666666” width=“50%”><input type=“text” name=“logoCurrent” size=“50” value=“<? echo $path;?>” /> <a href=“#” style=“color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;”>View Image</a></td>
</tr>
<? }
?>
<tr>
<td bgcolor=“#666666” width=“50%”>
<div style=“font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#ffffff; font-weight:bold; position:relative; float:left; left:10px;”>New Category Image:</div>
</td>
<td bgcolor=“#666666” width=“50%”><input type=“file” name=“logo” size=“50” value=“<? echo $path;?>” /></td>
</tr>

Then Im trying to put an if statement which says if the user hasnt browsed and changed the image then use the image path in the new place as below, but its not quite going to plan.


		// CODE FOR GETTING IMAGE DETAILS 		
		$name=$_POST['txtname'];
		$desc1=$_POST['txtdesc'];	
		$src=$_FILES['logo']['tmp_name'];
		$fname=$_FILES['logo']['name'];
		$fname2=$_POST['logoCurrent'];
		$src2=$_POST['logoCurrent'];
	
		$sr=$_POST['srno1'];
		$name=$_POST['txtname'];
		$desc=$_POST['txtdesc'];

		// CODE FOR UPDATING IMAGE IN /IMGDATA/CHAIN/<IMAGENAME> FOLDER
		if($_POST['logo']=="imgdata/category/$fname") {
			copy("$src","imgdata/category/$fname");
			$q24=mysql_query("update category set category_Name='$name', category_Image='imgdata/category/$fname', category_Description='$desc1' where cat_Id=$sr") or die (mysql_error());
			$flag=1;
			$conf="Data Updated Successfully 1";
			} else {
			copy("$src","imgdata/category/$fname2");
			$q24=mysql_query("update category set category_Name='$name', category_Image='imgdata/category/$fname2', category_Description='$desc1' where cat_Id=$sr") or die (mysql_error());
			$flag=1;
			$conf="Data Updated Successfully 2";
			}


I’ll keep trying