我有一个产品列表,我可以添加,更新,删除产品。添加,删除工作正常。当我更新产品时,除了图像路径之外,一切都在更新。它只需删除旧文件路径并更新空值。即使我尝试编辑产品,其他值也可以正确地从DB中获取,但文件路径除外,它只显示"没有选择文件"。实际上应该显示文件路径没有?所以,当我尝试发布此表单值时,它只给出空值。请有人帮我解决这个问题。 在这里,我到目前为止所做的一切。 在此先感谢。
Products.php
<?php include_once("auth.php"); ?>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<table border="solid 1px black;">
<tr><td colspan="6" align="right"><a href="product_form.php">Add new</a></td></tr>
<tr>
<th>S. No</th>
<th>Product Code</th>
<th>Name</th>
<th>Description</th>
<th>Image</th>
<th>Price</th>
<th>Available Qunatity</th>
<th>Date</th>
<th>Operations</th>
</tr>
<tbody>
<?php
if(isset($_REQUEST['delete_id'])) {
$query = mysql_query("DELETE FROM products WHERE id =".$_REQUEST['delete_id']);
header("Location:products.php");
} else if(isset($_POST) && isset($_POST['product_code'])){
$temp = explode(".", $_FILES["product_img_name"]["name"]);
$extension = end($temp);
if ((($_FILES["product_img_name"]["type"] == "image/gif")
|| ($_FILES["product_img_name"]["type"] == "image/jpeg")
|| ($_FILES["product_img_name"]["type"] == "image/jpg")
|| ($_FILES["product_img_name"]["type"] == "image/pjpeg")
|| ($_FILES["product_img_name"]["type"] == "image/x-png")
|| ($_FILES["product_img_name"]["type"] == "image/png"))) {
if ($_FILES["product_img_name"]["error"] > 0) {
echo "Return Code: " . $_FILES["product_img_name"]["error"] . "<br>";
} else {
if (file_exists("images/" . $_FILES["product_img_name"]["name"])) {
echo $_FILES["product_img_name"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["product_img_name"]["tmp_name"],
"images/" . $_FILES["product_img_name"]["name"]);
$path = "images/". $_FILES["product_img_name"]["name"];
}}}
$id = $_POST['id'];
$product_code = $_POST['product_code'];
$product_name = $_POST['product_name'];
$product_desc = $_POST['product_desc'];
$image = mysql_query(" SELECT product_img_name FROM products WHERE id='$id'");
$image_fetch= mysql_fetch_array($image);
$oldimage= $image_fetch['product_img_name'];
#################### Is this correct ? Why its not working??? ########
if(isset($_FILES["product_img_name"]["name"]) AND ($_FILES["product_img_name"]["name"]) ==''){
$product_img_name = $oldimage;
}
else{
$product_img_name = $path;
}
#########################
$price = $_POST['price'];
$available_qty = $_POST['available_qty'];
$date = date( 'd-m-y h:i:s');
if($id == '') {
$query = mysql_query("INSERT INTO products (`id`,`product_code`, `product_name`,`product_desc`, `product_img_name`, `price`,`available_qty`, `date`) VALUES ('$id','$product_code', '$product_name','$product_desc', '$product_img_name', '$price','$available_qty', '$date')");
} else { $query = mysql_query("UPDATE products SET `product_code`='$product_code', `product_name` = '$product_name', `product_desc` ='$product_desc', `product_img_name` = '$product_img_name', `price` = '$price', `available_qty` = '$available_qty', `date` = '$date' WHERE id = $id"); }
if($query) { header("Location:products.php"); } else { header("Location:product_form.php"); }
}
else {
$query = mysql_query("SELECT * FROM products");
while($result = mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['product_code']; ?></td>
<td><?php echo $result['product_name']; ?></td>
<td><?php echo $result['product_desc']; ?></td>
<td><img src="<?php echo $result['product_img_name']; ?>" height="100px" width="100px"></td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['available_qty']; ?></td>
<td><?php echo $result['date']; ?></td>
<td><a href="product_form.php?eidt_id=<?php echo $result['id']; ?>">Edit</a> | <a href="products.php?delete_id=<?php echo $result['id']; ?>">Delete</a></td>
</tr>
<?php
} }
?>
</tbody>
</table>
</body>
</html>
product_form.php
<?php include_once("auth.php"); ?>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
if (isset($_REQUEST['eidt_id'])) {
$query = mysql_query("SELECT * FROM products WHERE id =".$_REQUEST['eidt_id']);
$result = mysql_fetch_array($query);
$id = $result['id'];
$product_code = $result['product_code'];
$product_name = $result['product_name'];
$product_desc = $result['product_desc'];
$product_img_name = $result['product_img_name'];
$price = $result['price'];
$available_qty = $result['available_qty'];
$date = $result['date'];
} else {
$id = '';
$product_code = '';
$product_name = '';
$product_desc = '';
$product_img_name = '';
$price = '';
$available_qty = '';
$date = '';
}
?>
<form action="products.php" method="post" enctype="multipart/form-data">
<label>Product Code</label>
<input type="text" name="product_code" value="<?=$product_code?>" ><br/>
<label>Name</label>
<input type="text" name="product_name" value="<?=$product_name?>" ><br/>
<label>Description</label>
<input type="text" name="product_desc" value="<?=$product_desc?>"><br/>
<label>Upload Image</label>
<img src="<?=$product_img_name?>">
<input type="file" name="product_img_name" value="<?=$product_img_name?>"><br/>
<label>Price</label>
<input type="text" name="price" value="<?=$price?>"><br/>
<label>Available Quantity</label>
<input type="text" name="available_qty" value="<?=$available_qty?>"><br/>
<input type="hidden" name="id" value="<?=$id?>">
<input type="submit" value="Save">
</form>
</body>
</html>