나는 이미지를 폴더에 업로드하는 PHP 페이지가 있으며 이름은 mysql 테이블에 삽입된다. 이제 그림을 업데이트하고 디렉토리 폴더에서 이전 그림을 삭제하거나 이전 그림을 새 그림으로 바꾸는 페이지를 만들고 싶습니다.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;
}
?>
여기 난 당신이
enctype="multipart/form-data"
파일 작업을 그리워 생각 형태
<?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"> </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를 사용해야합니다. – Bono
PHP에 대해 많이 모른다. 필자는 튜토리얼을 따라 삽입 부분을 읽었고 튜토리얼의 일부가 아닌 업데이트를 시도했다. @Bono – user1084949