Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To Update An Image In Mysql
    primarykey
    data
    text
    <p>I have a php page that upload image to a folder and the name is inserted in mysql table . Now I want to create a page that will update the picture and delete the old picture in the directory folder or replace the old with the new picture.</p> <p>Here is the code which is not updating not the image or the other fields.</p> <pre><code> &lt;?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; } ?&gt; </code></pre> <p>here is the form</p> <pre><code> &lt;?php require_once('Connections/shopping.php'); ?&gt; &lt;?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); ?&gt; &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt; &lt;/head&gt; &lt;body&gt; &lt;div align="center"&gt; &lt;form method="post" name="form1" action="updateupload.php"&gt; &lt;table align="center"&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Serial:&lt;/td&gt; &lt;td&gt;&lt;?php echo $row_Recordset1['serial']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Name:&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="name" value="&lt;?php echo $row_Recordset1['name']; ?&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="description" value="&lt;?php echo $row_Recordset1['description']; ?&gt;" size="32"&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 $row_Recordset1['price']; ?&gt;" size="32"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="baseline"&gt; &lt;td nowrap align="right"&gt;Picture:&lt;/td&gt; &lt;td&gt;&lt;input type="file" name="picture" value="&lt;?php echo $row_Recordset1['picture']; ?&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 name="submit" type="submit" value="Update record"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php mysql_free_result($Recordset1); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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