mysql更新php图片,php-如何在Mysql中更新图像

我有一个php页面,可将图像上传到一个文件夹,并将名称插入mysql表中.现在,我想创建一个页面,该页面将更新图片并删除目录文件夹中的旧图片,或者将旧图片替换为新图片.

这是不更新图像或其他字段的代码.

// 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 failures 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;

}

?>

这是表格

$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);

$totalRows_Recordset1 = mysql_num_rows($Recordset1);

?>

Untitled Document
Serial:<?php echo $row_Recordset1['serial']; ?>
Name:
Description:
Price:
Picture:
 

mysql_free_result($Recordset1);

?>

解决方法:

我想你想念

enctype="multipart/form-data"

处理文件.

标签:image,mysql,php,image-uploading

来源: https://codeday.me/bug/20191201/2079501.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值