大佬教程收集整理的这篇文章主要介绍了php-如何在Mysql中更新图像,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个PHP页面,可将图像上传到一个文件夹,并将名称插入mysql表中.现在,我想创建一个页面,该页面将更新图片并删除目录文件夹中的旧图片,或者将旧图片替换为新图片.
这是不更新图像或其他字段的代码.
<?PHP
// Start a session for error reporTing
session_start();
// Call our connection file
require("includes/conn.PHP");
// Set some constants
// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a Trailing forWARD slash
$TARGET_PATH = "images/";
// Get our POSTed variables
$name = $_POST['name'];
$description = $_POST['description '];
$price = $_POST['price'];
$image = $_FILES['image'];
$serial = $_POST['serial'];
// Sanitize our inputs
$name = MysqL_real_escape_String($Name);
$description = MysqL_real_escape_String($description);
$price = MysqL_real_escape_String($pricE);
$image['name'] = MysqL_real_escape_String($image['name']);
// Build our target path full String. This is where the file will be moved do
// i.e. images/picture.jpg
$TARGET_PATH .= $image['name'];
// Here we check to see if a file with that name already exists
// You Could get past filename problems by appending a timestamp to the filename and then conTinuing
if (file_exists($TARGET_PATH))
{
$_SESSION['error'] = "A file with that name already exists";
header("LOCATIOn: updateproduct.PHP");
exit;
}
// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
// NOTE: This is where a lot of people make mistakes.
// We are *not* putTing the imagE into the database; we are putTing a reference to the file's LOCATIOn on the server
$sql = "updatE products SET picture = '$image', description = '$description' ,price = '$price' ,name = '$name' WHERE serial = '$serial'";
$result = MysqL_query($sql) or die ("Could not insert data into DB: " . MysqL_error());
header("LOCATIOn: updateproduct.PHP");
exit;
}
else
{
// A common cause of file moving @R_598_4895@ is because of bad permissions on the directory attempTing to be written to
// Make sure you chmod the directory to be writeable
$_SESSION['error'] = "Could not upload file. check read/write persmissions on the directory";
header("LOCATIOn: updateproduct.PHP");
exit;
}
?>
这是表格
<?PHP require_once('Connections/shopping.PHP'); ?>
<?PHP
$colname_Recordset1 = "1";
if (isset($_POST['serial'])) {
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $_POST['serial'] : addslashes($_POST['serial']);
}
MysqL_SELEct_db($database_shopping, $shopping);
$query_Recordset1 = sprintf("SELECT * FROM products WHERE serial = %s", $colname_Recordset1);
$Recordset1 = MysqL_query($query_Recordset1, $shopping) or die(MysqL_error());
$row_Recordset1 = MysqL_fetch_assoc($Recordset1);
$@R_403_10586@lRows_Recordset1 = MysqL_num_rows($Recordset1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<Meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center">
<form method="post" name="form1" action="updateupload.PHP">
<table align="center">
<tr valign="baseline">
<td Nowrap align="right">serial:</td>
<td><?PHP echo $row_Recordset1['serial']; ?></td>
</tr>
<tr valign="baseline">
<td Nowrap align="right">Name:</td>
<td><input type="text" name="name" value="<?PHP echo $row_Recordset1['name']; ?>" size="32"></td>
</tr>
<tr valign="baseline">
<td Nowrap align="right">Description:</td>
<td><input type="text" name="description" value="<?PHP echo $row_Recordset1['description']; ?>" size="32"></td>
</tr>
<tr valign="baseline">
<td Nowrap align="right">Price:</td>
<td><input type="text" name="price" value="<?PHP echo $row_Recordset1['price']; ?>" size="32"></td>
</tr>
<tr valign="baseline">
<td Nowrap align="right">Picture:</td>
<td><input type="file" name="picture" value="<?PHP echo $row_Recordset1['picture']; ?>" size="32"></td>
</tr>
<tr valign="baseline">
<td Nowrap align="right"> </td>
<td><input name="submit" type="submit" value="update record"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<?PHP
MysqL_free_result($Recordset1);
?>
解决方法:
我想你想念
enctype="multipart/form-data"
处理文件.
http://www.w3schools.com/php/php_file_upload.asp
以上是大佬教程为你收集整理的php-如何在Mysql中更新图像全部内容,希望文章能够帮你解决php-如何在Mysql中更新图像所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。