Page 1 of 1

insert,update,delete form problem when updating img

Posted: Wed Aug 23, 2006 7:25 pm
by laura
hi,i am doing this insert,update,delete stuffs. for eg the user can insert categoryName,Img,Comment. after that,the user can edit the stuffs they inserted.

here comes the problem,when inserting,everything is fine.but when the user click to edit(which is going to the edit page)the form value i can get are only categoryid,categoryName,Comment,but not the image.

so each time when the user edit,they have to insert the image.and then the folder,the old image is still exist and the new image is inserted to the folder.

how do i make so that the user when they click to edit,the img value is exist in the edit page?and if they dun change the img,then the img in the folder will not delete,and if they change,then the old image deleted and new image inserted?pls..start a little bit of coding and explaining it..so that i could continue doing it..really appreicate who helps me!!!!!thanks in advance!!!!!!!

*if my question is difficult to understd,pls ask me to type again(my english not so good)
here are my code,hope anyone who knows could help me out..^^v

Code: Select all

<?php
//check session
session_start();
print $_SESSION["UID"]; //check got uid or not
if(!$_SESSION["User"])
{
echo "need login";
echo "<a href='login.php'>click here</a>";
exit;
}
?>
<html>
<head>
	<title>Insert_Cat.php</title>
</head>
<body>
<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> 
CategoryName: <input type="text" name="cat"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="2048000">
Img: <input name="userfile" type="file"><br>
Comment: <textarea name="comment"></textarea><br>
<input type="submit" name="send" value="Send">
</form>
<?php
include "IncludeFiles/db_config.php";
//step 1: connect to host
$myConn=mysql_connect($host,$uname,$pword) or die("cannot connect host");
//step 2: select database
$mydb=mysql_select_db($dbname,$myConn) or die("cannot select db");
if($HTTP_POST_FILES['userfile']['size']<=0)
{ 
echo "no img<br>";
} 
else 
{
$tempFile=$HTTP_POST_FILES['userfile']['tmp_name'];
$finalFile=$HTTP_POST_FILES['userfile']['name'];
$destFile="images/".$finalFile;
copy($tempFile,$destFile);
echo "i got ur file<br>";
}
$cat=$_POST["cat"];
$img=$finalFile;
$comment=$_POST["comment"];	
//if(isset($cat)&&($img)&&($comment))
////{
	///$SQL="INSERT INTO categories VALUES(null,'$cat','$img','$comment')";
	//$rs=mysql_query($SQL,$myConn);
//}
////else
//{
//echo "shiko!";
//}


if(($_POST["cat"]==null)||($img==null)||($_POST["comment"]==null))
//if(isset($_POST["cat"]) && isset($_POST["img"]) && isset($_POST["comment"]))
{
echo "need fill";
}
//step 3: issue SQL statement
else 
{
$SQL="INSERT INTO categories VALUES(null,'$cat','$img','$comment')";
print $SQL;
//step 4: result set
$rs=mysql_query($SQL,$myConn);
if($rs)
{
echo "Good job, ".mysql_affected_rows()." info inserted into the table";
}
else
{
echo "error with SQL";
}
}
//display table
$sql_display="select * from categories order by CategoryID desc";
echo "<br>";
echo $sql_display;
$result=mysql_query($sql_display,$myConn);
?>
<?php
echo "<table border=1>";
echo "<tr>";
echo "<td>";
echo "CategoryID";
echo "</td>";
echo "<td>";
echo "CategoryName";
echo "</td>";
echo "<td>";
echo "Img";
echo "</td>";
echo "<td>";
echo "Comment";
echo "</td>";
echo "</tr>";
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo "<a href='edit_Cat.php?fid=".$row["CategoryID"]."'> ".$row["CategoryID"]."</a>";
echo "</td>";
echo "<td>";
echo $row["CategoryName"];
echo "</td>";
echo "<td>";
echo "<img src='images/".$row["Img"]."' width=50 height=50>";
echo "</td>";
echo "<td>";
echo $row["Comment"];
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($myConn);
?>
<a href="logoff.php">log out</a><br><br>
<a href="login.php">back</a>
</body>
</html>

Code: Select all

<?php session_start(); ?>
<html>
<head>
<title>
edit_cat.php
</title>
</head>
<body>
<?php
include "IncludeFiles/db_config.php";
//step 1: connect to host
$myConn=mysql_connect($host,$uname,$pword) or die("cannot connect host");
//step 2: select database
$mydb=mysql_select_db($dbname,$myConn) or die("cannot select db");
//step 3: issue SQL statement
$sql="select * from categories where CategoryID=".$_GET["fid"];
echo $sql;
//step4: result set
$rs=mysql_query($sql,$myConn);
//step5:display $rs
echo "<form enctype='multipart/form-data' action='".$_SERVER["PHP_SELF"]."' method='post'> ";
$row=mysql_fetch_array($rs);
echo "<input type='hidden' name='categoryID' value='".$row["CategoryID"]."'><br>";
echo "CategoryName";     echo "<input type='text' name='categoryname' value='".$row["CategoryName"]."'><br>";
echo "Img";       echo "<input name='userfile' type='file'><br>";
echo "Comment";     echo "<input type='text' name='comment' value='".$row["Comment"]."'><br>";
echo "<input type='submit' name='update' value='update'>";
echo "<input type='submit' name='delete' value='delete' >";
echo"</form>";
if($HTTP_POST_FILES['userfile']['size']<=0)
{ 
echo "no img<br>";
} 
else 
{
$tempFile=$HTTP_POST_FILES['userfile']['tmp_name'];
$finalFile=$HTTP_POST_FILES['userfile']['name'];
$destFile="images/".$finalFile;
copy($tempFile,$destFile);
echo "i got ur file<br>";
}


if(isset($_POST["update"]))
{
echo "do update<br>";
$sql_up="update categories set CategoryName='".$_POST["categoryname"]."',Img='".$finalFile."',Comment='".$_POST["comment"]."' where CategoryID=".$_POST["categoryID"];
print $sql_up;
$rs=mysql_query($sql_up,$myConn);
}
else if(isset($_POST["delete"]))
{
echo "do delete<br>";
$sql_del="delete from categories where CategoryID=".$_POST["categoryID"];
print $sql_del;
$rs=mysql_query($sql_del,$myConn);
}
mysql_close($myConn);
?><a href="Insert_Cat.php">back</a>
</body>
</html>

Posted: Wed Aug 23, 2006 7:30 pm
by volka
You can split it up into two forms on the same page. One for the text data and one for the image file.

Posted: Wed Aug 23, 2006 7:44 pm
by laura
thank you for your fast reply.
but that is the problem,cos i dunno how to get the value when go to the next page,
and then,delete.i know i hav to use 'unlink' but i not sure about that..
that's y i wish someone who knows..could start a little bit of coding..so i could continue with it..(cos i am really really bad in coding)
sorry for the incouvenience!! but really thanks for your fast response!!!!!!!!!!!!

Posted: Wed Aug 23, 2006 7:53 pm
by volka
You cannot set a default value for input/file elements.
But you can show the current image and have a blank input/file element in case the user want to upload another picture.

Take a look at your forum profile, profile.php?mode=editprofile
and set/edit/delete your avatar picture as example.

Posted: Wed Aug 23, 2006 7:56 pm
by RobertGonzalez
I am not sure anyone will start the code for you. We will help you with logic though. I would suggest one form, but with a hidden field that has a value of either 'edit' or 'add'. Depending on which is in the form, your script will determine what to do with it. I would also suggest that you do nothing with the image that is already in the database. Only do something if the user wants to override the image with another. Then all you have to do is check for whether there is a passed file to upload instead of handling what wasn't added. You could also add a checkbox for 'delete' so if the user wants to delete the image, they can check the box and you can delete the image that is stored.

Posted: Wed Aug 23, 2006 9:16 pm
by laura
thank you everyone!!!!!!!!!!
i will try and see!!!!!!!!!
actually i dun hav problem with the database when the user upload the img,or when the user click edit,and upload the img again.
the problem is only in the images folder..

but anyway..let me digest first!!!!(slow learner)
thanks a lot !!!!!!!!!!!!!!!!!!!

Posted: Thu Aug 24, 2006 1:13 am
by laura
vodka..can i ask u sth?

i tried my profile..
then i upload one my img to this forum..
and then yes i can see the pic..
so i tired to edit my profile..
then i din't select the delete check box and i just upload another pic..
is this means the old img is still in the XXX <--sorry i dunno what should i say for this. 'file for upload img' or 'something'

thanks !!!!!!!

Posted: Thu Aug 24, 2006 2:18 am
by volka
don't know, I haven't programmed that feature.
And you can do it as you like or need.