2012-05-31 43 views
1

我有一個將圖像上傳到文件夾並將名稱插入到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 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); 
?> 
+0

請停止與古mysql_ *函數編寫新的代碼。他們不再被維護,社區已經開始了貶值過程。相反,您應該瞭解準備好的聲明並使用PDO或MySQLi。 – Bono

+0

我不知道很多關於PHP我跟着一個教程插入部分,並基於這一點,我試圖做的更新,這不是教程的一部分@Bono – user1084949

回答

1

我想你錯過了

enctype="multipart/form-data" 

與文件。

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

+0

oooh我怎麼忘記我會嘗試它,讓你知道 – user1084949

+0

我添加了enctype =「multipart/form-data」,但它沒有工作,,, plz help – user1084949

+0

@ user1084949 in this line「$ image = $ _FILES ['image'];」爲什麼你有'圖像',如果字段名稱是'圖片'? – jcho360

1

unlink功能可能只是你想要的

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

@ user1084949您可以從mysql表中找到$ PATH_TO_IMAGE,因爲您在圖表中存儲了圖像路徑。歡呼:) –

+0

@Subail Sunny我在表格中存儲圖像名稱(product.gif)而不是路徑...它自己保存在名爲images的文件夾中的圖片。 – user1084949

+1

@ user1084949如果所有圖像都存儲在同一文件夾中,則所有圖像均爲$ PATH_TO_IMAGE = $ folderpath。'/ imagename';會做這項工作 –