【问题标题】:How To Update An Image In Mysql如何在 Mysql 中更新图像
【发布时间】:2012-06-05 18:51:15
【问题描述】:

我有一个 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 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;
}
?>

这是表格

   <?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);
$totalRows_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">&nbsp;</td>
        <td><input name="submit" type="submit" value="Update record"></td>
      </tr>
    </table>
  </form>
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

【问题讨论】:

  • 请停止使用古老的 mysql_* 函数编写新代码。它们不再被维护并且社区已经开始了弃用过程。相反,您应该了解准备好的语句并使用 PDO 或 MySQLi。
  • 我对 php 了解不多,我按照插入部分的教程进行操作,并在此基础上尝试进行不属于教程的更新@Bono

标签: php mysql image image-uploading


【解决方案1】:

我觉得你错过了

enctype="multipart/form-data"

处理文件。

http://www.w3schools.com/php/php_file_upload.asp

【讨论】:

  • 哦,我怎么忘了我会尝试并告诉你
  • 我添加了 enctype="multipart/form-data" 但它没有工作 ,,, 请帮助
  • @user1084949 在这一行 "$image = $_FILES['image'];"如果字段名称是“图片”,为什么还有“图片”?
【解决方案2】:

unlink 函数可能正是您想要的

if (file_exists($PATH_TO_IMAGE))
{
       unlink($PATH_TO_IMAGE);            
}

【讨论】:

  • @user1084949 您可以从 mysql 表中找到 $PATH_TO_IMAGE ,因为您将图像路径存储在表中。干杯:)
  • @Subail Sunny 我将图像名称 (product.gif) 存储在表中而不是路径中......它自己的图片保存在名为 images 的文件夹中。
  • @user1084949 all 如果所有图像都存储在同一个文件夹中,则 $PATH_TO_IMAGE = $folderpath.'/imagename';会做的工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-22
  • 2021-02-05
相关资源
最近更新 更多