我无法从MySQL更新图片,我使用BLOB存储图片但我无法更新它们。我可以创建(上传)一张图片,我也可以阅读(显示)它,但我怎么也不知道如何更新它。
以下是我更新的代码.php
<?php
session_start();
$con=@mysql_connect("localhost","root","12148qx3er");
$dbcheck = mysql_select_db("buybranded");
if (!$dbcheck) {
echo mysql_error();
}
$itemid = $_GET['id'];
$image = addslashes(file_get_contents($_FILES['prod_pic']['tmp_name']));
$sql = "UPDATE `inventory` SET
prod_brand = '$_POST[prod_brand]',
prod_name = '$_POST[prod_name]',
prod_category = '$_POST[prod_category]',
prod_price = '$_POST[prod_price]',
prod_desc = '$_POST[prod_desc]',
prod_quantity = '$_POST[prod_quantity]','{$image}', WHERE prod_id=$itemid";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error($con));
}
header('refresh: 0;url=inventory.php');
$message = "Product Successfully Updated";
echo("<script type='text/javascript'>alert('$message');</script>");
?>
以及后续问题,当我在没有新文件的情况下按更新时,它说file_get_contents()不能为空。也许我的代码是错误的,我只需要在需要时更新条目。我可以更新表格,但不能更新图片。
这里是完整的update_inv.php代码
<html>
<head>
<meta charset="utf-8" />
<title>Edit a Product</title>
<link rel="stylesheet" type="text/css" href="cms.css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script>
$(document).ready(function(){
$("#prod_brand").autocomplete("prod_brand_auto_complete.php", {
selectFirst: true
});
});
</script>
</head>
<body>
<style>
button {
margin: 10px;
padding: 10px;
}
a{
color: black;
text-decoration: none;
}
</style>
<?php
session_start();
$conn = @mysql_connect("localhost","root","12148qx3er");
$db = @mysql_select_db("buybranded");
$itemid = $_GET['id'];
$qry = "SELECT * FROM inventory where prod_id = ".$itemid;
mysql_set_charset("UTF8");
$result = @mysql_query($qry);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
echo "<div id='page-wrap'>
<h1>Edit a Product</h1>";
while ($row=mysql_fetch_array($result)){
$prodbrand = $row['prod_brand'];
$prodname = $row['prod_name'];
$prodcategory = $row['prod_category'];
$prodprice = $row['prod_price'];
$prod_desc = $row['prod_desc'];
$prodpic = $row['prod_pic'];
$prodquantity = $row['prod_quantity'];
echo "<form enctype='multipart/form-data' method='post' action='updating_inv.php?id=$itemid'>";
echo "<table>
<tr>";
echo "<td><label for='prod_brand'>Product Brand:</label></td>
<td><input name='prod_brand' id='prod_brand' type='text' maxlength='150' class='col1' placeholder='Auto Complete Form' value='$prodbrand'/></td>";
echo "<td><label for='prod_price'>Product Name:</label></td>
<td><input name='prod_name' id='prod_name' type='text' maxlength='150' class='col1' value='$prodname'/></td>
</tr>";
echo "<tr>";
echo "<td><label for='prod_price'>Product Price:</label></td>
<td><input name='prod_price' id='prod_price' type='text' maxlength='10' class='col1' value='$prodprice'/></td>";
echo "<td> <label for='prod_category'>Category:</label></td>";
if($prodcategory=="Compression"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option selected>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Headwear"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option selected>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Shorts"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option selected>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Yoga"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option selected>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Swimming"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option selected>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Fitness Training"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option selected>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Water Bottles"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option selected>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Sports Socks"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option selected>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Sports Bags"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option selected>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Protein Shaker"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option selected>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Gloves"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option selected>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Splash Guards"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option selected>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Massagers"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option selected>Massagers</option>
<option>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Sun Blocks"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option selected>Sun Blocks</option>
<option>Sports Cleaners</option>
</select></td>";
}
else if($prodcategory=="Sports Cleaners"){
echo "<td><label for='prod_category'>Category:</label>
<select name='prod_category' class='col1'>$prodcategory
<option>Select Category</option>
<option>Compression</option>
<option>Headwear</option>
<option>Shorts</option>
<option>Yoga</optino>
<option>Swimming</option>
<option>Fitness Training</option>
<option>Water Bottles</option>
<option>Sports Socks</option>
<option>Sports Bags</option>
<option>Protein Shaker</option>
<option>Gloves</option>
<option>Splash Guards</option>
<option>Massagers</option>
<option>Sun Blocks</option>
<option selected>Sports Cleaners</option>
</select></td>";
}
echo "</tr>";
echo "<tr>";
echo "<td><label for='prod_desc'>Description:</label></td>
<td><textarea name='prod_desc' id='prod_desc' class='col2'>$prod_desc</textarea></td>";
echo "<td></td><td><table><tr>";
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['prod_pic'] ) . '" width="100" border="1px"/><br>';
echo "<td><label for='prod_pic'>Upload Image:</label><br><input type='file' name='prod_pic' class='col2' value=''/></td>";
echo "</tr>";
echo "<tr>";
echo "<td><label for='prod_quantity'>Quantity:</label><br><input name='prod_quantity' id='prod_quantity' type='text' maxlength='10' class='col2' placeholder='ex. 10' value='$prodquantity'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td><form method='post'><button>Update this Entry!</button>";
echo "<button><a href='javascript:history.back()'>Go back</a></button></td>";
echo "</tr></table></td></table>";
echo "</form>";
}
echo "</div>";
?>
</div>
</body>
</html>