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>