Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate Images On Database Using a PHP FORM
    text
    copied!<p>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 the "Description" 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, but if i upload a new image, it will be replaced with the new one. as the form stands, the only way to update the database entry is to re-upload the images each time anything is updated. this won't be practical when the site is running though as it will need to be updated from different devices, and the images won't be on every device. apologies if this sounds like a stupid question, but i'm new to php and i've been stuck on this for days now. i'm sure it's probably something simple, but here's the script i have for the upload:</p> <pre><code>$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"])) &amp;&amp; ($_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")); </code></pre> <p>and here's the code i have for the form:</p> <pre><code> &lt;form method="post" name="form1" action="&lt;?php echo $editFormAction; ?&gt;" enctype="multipart/form-data"&gt; &lt;table align="center"&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Product ID:&lt;/td&gt; &lt;td&gt; &lt;?php echo $row_RS_updateProducts['product_id']?&gt; &lt;/td&gt; &lt;tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Thumb:&lt;/td&gt; &lt;td&gt;&lt;?php echo $row_RS_updateProducts['thumb']?&gt;&lt;input type="file" name="thumb" value="&lt;?php echo $row_RS_updateProducts['thumb']?&gt;" size="32"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Image 1:&lt;/td&gt; &lt;td&gt;&lt;?php echo $row_RS_updateProducts['image_1']?&gt;&lt;input type="file" name="image_1" value="&lt;?php echo $row_RS_updateProducts['image_1']?&gt;" size="32"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Image 2:&lt;/td&gt; &lt;td&gt;&lt;?php echo $row_RS_updateProducts['image_2']?&gt;&lt;input type="file" name="image_2" value="&lt;?php echo $row_RS_updateProducts['image_2']?&gt;" size="32"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Title:&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="title" value="&lt;?php echo htmlentities($row_RS_updateProducts['title'], ENT_COMPAT, 'UTF-8'); ?&gt;" size="32"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Description:&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="desc" value="&lt;?php echo htmlentities($row_RS_updateProducts['desc'], ENT_COMPAT, 'UTF-8'); ?&gt;" size="32"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Category:&lt;/td&gt; &lt;td&gt;&lt;select name="category_id"&gt; &lt;?php do { ?&gt; &lt;option value="&lt;?php echo $row_RS_Category['category_id']?&gt;" &lt;?php if (!(strcmp($row_RS_Category['category_id'], htmlentities($row_RS_updateProducts['category_id'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?&gt;&gt;&lt;?php echo $row_RS_Category['category_name']?&gt;&lt;/option&gt; &lt;?php } while ($row_RS_Category = mysql_fetch_assoc($RS_Category)); ?&gt; &lt;/select&gt;&lt;/td&gt; &lt;tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Status:&lt;/td&gt; &lt;td&gt;&lt;select name="status"&gt; &lt;option value="1" &lt;?php if (!(strcmp(1, htmlentities($row_RS_updateProducts['status'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?&gt;&gt;Available&lt;/option&gt; &lt;option value="2" &lt;?php if (!(strcmp(2, htmlentities($row_RS_updateProducts['status'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?&gt;&gt;Pending&lt;/option&gt; &lt;option value="3" &lt;?php if (!(strcmp(3, htmlentities($row_RS_updateProducts['status'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?&gt;&gt;Unavailable&lt;/option&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Shipping Band:&lt;/td&gt; &lt;td&gt;&lt;select name="shipping_band"&gt; &lt;option value="a" &lt;?php if (!(strcmp("a", htmlentities($row_RS_updateProducts['shipping_band'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?&gt;&gt;A&lt;/option&gt; &lt;option value="b" &lt;?php if (!(strcmp("b", htmlentities($row_RS_updateProducts['shipping_band'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?&gt;&gt;B&lt;/option&gt; &lt;option value="c" &lt;?php if (!(strcmp("c", htmlentities($row_RS_updateProducts['shipping_band'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?&gt;&gt;C&lt;/option&gt; &lt;option value="d" &lt;?php if (!(strcmp("d", htmlentities($row_RS_updateProducts['shipping_band'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?&gt;&gt;D&lt;/option&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Price:&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="price" value="&lt;?php echo htmlentities($row_RS_updateProducts['price'], ENT_COMPAT, 'UTF-8'); ?&gt;" size="32"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&lt;input type="submit" value="Update record"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;input type="hidden" name="MM_update" value="form1"&gt; &lt;input type="hidden" name="product_id" value="&lt;?php echo $row_RS_updateProducts['product_id']; ?&gt;"&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload