2013-07-23 35 views
1

我正在創建一個內容管理系統,以便數據庫可以輕鬆更新。我創建了一個更新頁面,允許用戶編輯數據庫中的條目,他們可以上傳新圖像並編輯任何字段,而不會有任何問題。 我遇到的問題是,如果您只想編輯一個字段中的文本,但您不想更改圖像,那麼在提交表單時會用空白條目覆蓋圖像字段。 我希望能夠做的是爲原始圖像保留,如果沒有其他文件被選中。 我知道我需要檢查一個文件是否被選中,但我似乎無法找到一種可行的方法。我敢肯定,這可能是一些簡單的,但這裏的腳本我有上傳:更新數據庫表單:當沒有選擇文件時,留下圖像字段爲空

 $editFormAction = $_SERVER['PHP_SELF']; 
if (isset($_SERVER['QUERY_STRING'])) { 
/* upload image script */ 

$thumb=basename($_FILES['thumb']['name']); 
if(move_uploaded_file($_FILES['thumb']['tmp_name'],"../shopImages/".$thumb)) 

$image1=basename($_FILES['image_1']['name']); 
if(move_uploaded_file($_FILES['image_1']['tmp_name'],"../shopImages/".$image1)) 

$image2=basename($_FILES['image_2']['name']); 
if(move_uploaded_file($_FILES['image_2']['tmp_name'],"../shopImages/".$image2)) 

/*end of script*/ 
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
} 


if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) { 
$updateSQL = sprintf("UPDATE tbl_products SET thumb=%s, image_1=%s, image_2=%s, title=%s, `desc`=%s, category_id=%s, status=%s, shipping_band=%s, price=%s WHERE product_id=%s", 
        GetSQLValueString($thumb, "text"), 
        GetSQLValueString($image1, "text"), 
        GetSQLValueString($image2, "text"), 
        GetSQLValueString($_POST['title'], "text"), 
        GetSQLValueString($_POST['desc'], "text"), 
        GetSQLValueString($_POST['category_id'], "int"), 
        GetSQLValueString($_POST['status'], "int"), 
        GetSQLValueString($_POST['shipping_band'], "text"), 
        GetSQLValueString($_POST['price'], "double"), 
        GetSQLValueString($_POST['product_id'], "int")); 

這裏是表單代碼:

<form method="post" name="form1" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data"> 
    <table align="center"> 
    <tr valign="baseline"> 
     <td nowrap align="right">Product ID:</td> 
     <td> 

    <?php echo $row_RS_updateProducts['product_id']?> 

    </td> 
<tr> 
<tr valign="baseline"> 
    <td nowrap align="right">Thumb:</td> 
    <td><?php echo $row_RS_updateProducts['thumb']?><input type="file" name="thumb" value="<?php echo $row_RS_updateProducts['thumb']?>" size="32"></td> 

</tr> 
<tr valign="baseline"> 
    <td nowrap align="right">Image 1:</td> 
    <td><?php echo $row_RS_updateProducts['image_1']?><input type="file" name="image_1" value="<?php echo $row_RS_updateProducts['image_1']?>" size="32"></td> 
</tr> 
<tr valign="baseline"> 
    <td nowrap align="right">Image 2:</td> 
    <td><?php echo $row_RS_updateProducts['image_2']?><input type="file" name="image_2" value="<?php echo $row_RS_updateProducts['image_2']?>" size="32"></td> 
</tr> 
<tr valign="baseline"> 
    <td nowrap align="right">Title:</td> 
    <td><input type="text" name="title" value="<?php echo htmlentities($row_RS_updateProducts['title'], ENT_COMPAT, 'UTF-8'); ?>" size="32"></td> 
</tr> 
<tr valign="baseline"> 
    <td nowrap align="right">Description:</td> 
    <td><input type="text" name="desc" value="<?php echo htmlentities($row_RS_updateProducts['desc'], ENT_COMPAT, 'UTF-8'); ?>" size="32"></td> 
</tr> 
<tr valign="baseline"> 
    <td nowrap align="right">Category:</td> 
    <td><select name="category_id"> 
    <?php 
do { 
?> 
    <option value="<?php echo $row_RS_Category['category_id']?>" <?php if (!(strcmp($row_RS_Category['category_id'], htmlentities($row_RS_updateProducts['category_id'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>><?php echo $row_RS_Category['category_name']?></option> 
    <?php 
} while ($row_RS_Category = mysql_fetch_assoc($RS_Category)); 
?> 
    </select></td> 
<tr> 
<tr valign="baseline"> 
    <td nowrap align="right">Status:</td> 
    <td><select name="status"> 
    <option value="1" <?php if (!(strcmp(1, htmlentities($row_RS_updateProducts['status'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>>Available</option> 
    <option value="2" <?php if (!(strcmp(2, htmlentities($row_RS_updateProducts['status'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>>Pending</option> 
    <option value="3" <?php if (!(strcmp(3, htmlentities($row_RS_updateProducts['status'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>>Unavailable</option> 
    </select></td> 
</tr> 
<tr valign="baseline"> 
    <td nowrap align="right">Shipping Band:</td> 
    <td><select name="shipping_band"> 
    <option value="a" <?php if (!(strcmp("a", htmlentities($row_RS_updateProducts['shipping_band'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>>A</option> 
    <option value="b" <?php if (!(strcmp("b", htmlentities($row_RS_updateProducts['shipping_band'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>>B</option> 
    <option value="c" <?php if (!(strcmp("c", htmlentities($row_RS_updateProducts['shipping_band'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>>C</option> 
    <option value="d" <?php if (!(strcmp("d", htmlentities($row_RS_updateProducts['shipping_band'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>>D</option> 
    </select></td> 
</tr> 
<tr valign="baseline"> 
    <td nowrap align="right">Price:</td> 
    <td><input type="text" name="price" value="<?php echo htmlentities($row_RS_updateProducts['price'], ENT_COMPAT, 'UTF-8'); ?>" size="32"></td> 
</tr> 
<tr valign="baseline"> 
    <td nowrap align="right">&nbsp;</td> 
    <td><input type="submit" value="Update record"></td> 
</tr> 
    </table> 
    <input type="hidden" name="MM_update" value="form1"> 
    <input type="hidden" name="product_id" value="<?php echo $row_RS_updateProducts['product_id']; ?>"> 
</form> 

我是新的PHP所以任何幫助將不勝感激,因爲我一直堅持這個好幾天了。

+0

任何人可以幫助我在這?我是新來的PHP和真的卡住它。我已經嘗試了評論中的所有建議,但似乎沒有任何工作。 – paul

回答

0

你在表單

<form name="form_name" action="actionpage.php" enctype="multipart/form-data" method="post"> 

,如果你還沒有寫ENCTYPE = 「的multipart/form-data的」 您的文件不會被上傳提及。

+0

是的,我有。文件上傳工作正常。這是當我不想更新問題的圖像。作爲形式站立,我必須再次上傳圖像每次我想要更新任何其他領域。 – paul

+0

我已經添加了表單的代​​碼,所以你可以看到。有任何想法嗎? – paul

0

檢查做移動文件邏輯

像在此之前的$ _FILES組件:

if(!empty($_FILES['thumb']['name'])){ 
    $thumb = basename($_FILES['thumb']['name']); 
    move_uploaded_file($_FILES['thumb']['tmp_name'],"../shopImages/".$thumb); 
} 
if(!empty($_FILES['image_1']['name'])){ 
    $image1 = basename($_FILES['image_1']['name']); 
    move_uploaded_file($_FILES['image_1']['tmp_name'],"../shopImages/".$image1); 
} 
if(!empty($_FILES['image_2']['name'])){ 
    $image2 = basename($_FILES['image_2']['name']); 
    move_uploaded_file($_FILES['image_2']['tmp_name'],"../shopImages/".$image2); 
} 

,並更改您的查詢,看起來像這樣:

$updateSQL = sprintf("UPDATE tbl_products SET title=%s, `desc`=%s, category_id=%s, status=%s, shipping_band=%s, price=%s ", 
       GetSQLValueString($_POST['title'], "text"), 
       GetSQLValueString($_POST['desc'], "text"), 
       GetSQLValueString($_POST['category_id'], "int"), 
       GetSQLValueString($_POST['status'], "int"), 
       GetSQLValueString($_POST['shipping_band'], "text"), 
       GetSQLValueString($_POST['price'], "double")); 

if(isset($thumb)){ 
    $updateSql .= sprintf(" thumb=%s", GetSQLValueString($thumb, "text")); 
} 

if(isset($image1)){ 
    $updateSql .= sprintf(" image_1=%s", GetSQLValueString($image1, "text")); 
} 

if(isset($image2)){ 
    $updateSql .= sprintf(" image_2=%s", GetSQLValueString($image2, "text")); 
} 

$updateSql .= sprintf(" WHERE product_id=%s", GetSQLValueString($_POST['product_id'], "int")); 
+0

嗨。謝謝回覆。我以前曾嘗試過這種方法,並可以使其工作。我剛剛按照你的建議嘗試過,但仍然沒有快樂。 – paul

+0

這會返回什麼? GetSQLValueString($ thumb,「text」), 如果我上傳的圖像將插入縮略圖的路徑到數據庫字段並將圖像文件上傳到您的查詢中,可能會在sprintf – SkarXa

+0

中插入void blank正確的文件夾。如果我在簡單更新數據庫時不瀏覽文件,則會用空白條目覆蓋先前上傳的圖像。 – paul

相關問題