更新数据库表单:未选择文件时将图像字段留空

时间:2021-05-17 08:58:08

i'm creating a content management system so that the database can easily be updated. i've created an update page that lets the user edit entries in the database, and they can upload new images and edit any of the fields without any problems. the problem i have, is that if you just want to edit, say just the text in one field, but you don't want to change the images, when the form is submitted, it overwrites the image field with a blank entry. what i want to be able to do is for the original image to remain if no other file is selected. i know that i need to check if a file has been selected but i can't seem to find a way of doing it that works. i'm sure it's probably something simple, but here's the script i have for the upload:

我正在创建一个内容管理系统,以便可以轻松更新数据库。我创建了一个更新页面,允许用户编辑数据库中的条目,他们可以上传新图像并编辑任何字段而不会出现任何问题。我遇到的问题是,如果您只是想编辑,只说一个字段中的文本,但您不想更改图像,则在提交表单时,它会用空白条目覆盖图像字段。我想要做的是,如果没有选择其他文件,原始图像将保留。我知道我需要检查文件是否已被选中,但我似乎无法找到一种有效的方法。我确定它可能很简单,但这是我上传的脚本:

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

here is the code for the form:

这是表单的代码:

<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>

i'm new to php so any help would be much appreciated as i've been stuck on this for days now.

我是php新手,所以任何帮助都会非常感激,因为我已经坚持了好几天了。

2 个解决方案

#1


0  

have you mentioned in your form

你有没有在表格中提到过

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

if you have not written enctype="multipart/form-data" your file will not be uploaded.

如果您还没有编写enctype =“multipart / form-data”,则不会上传您的文件。

#2


0  

Check the $_FILES components before doing the move files logic

在执行移动文件逻辑之前检查$ _FILES组件

Like this:

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

And Change your query to look like this:

并将您的查询更改为如下所示:

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

#1


0  

have you mentioned in your form

你有没有在表格中提到过

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

if you have not written enctype="multipart/form-data" your file will not be uploaded.

如果您还没有编写enctype =“multipart / form-data”,则不会上传您的文件。

#2


0  

Check the $_FILES components before doing the move files logic

在执行移动文件逻辑之前检查$ _FILES组件

Like this:

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

And Change your query to look like this:

并将您的查询更改为如下所示:

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