kunyomi 5 Newbie Poster

Hi guys, I'm trying to UPDATE the image that has been already in the database with another image using a form in PHP.

I got the uploading working all fine already. Just the edit part.

With this code, I get no error, but the MySQL table is not written with the new image name and the image does not get uploaded into the 'uploads' folder.

<?php
include ("dbConfig.php");
require ("check.php");

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $id2 = $_GET["id2"];
      $sql = "SELECT * FROM contacts WHERE id2=$id2";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
	  
      <form action="<?php $v=explode('?',$_SERVER['PHP_SELF']); echo $v[0]; ?>" method="post">
      <input type=hidden name="id2" value="<?php echo $myrow["id2"]; ?>">
   
      Name: <INPUT TYPE="text" NAME="name" VALUE="<?php echo $myrow["name"]; ?>" SIZE=30><br>
      Email: <INPUT TYPE="text" NAME="email" VALUE="<?php echo $myrow["email"]; ?>" SIZE=30><br>
      Who: <INPUT TYPE="text" NAME="age" VALUE="<?php echo $myrow["age"]; ?>" SIZE=30><br>
      Birthday: <INPUT TYPE="text" NAME="birthday" VALUE="<?php echo $myrow["birthday"]; ?>" SIZE=30><br>
      Address: <TEXTAREA NAME="address" ROWS=10 COLS=30><?php echo $myrow["address"]; ?></TEXTAREA><br>
      Number: <INPUT TYPE="text" NAME="number" VALUE="<?php echo $myrow["number"]; ?>" SIZE=30><br>
      Contact Image: <INPUT NAME="uploadedfile" VALUE="<?php echo $myrow["uploadedfile"]; ?>" TYPE="file" /><br>

   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
      
<?php      
   }
}
$target = "C:/Program Files/xampp/htdocs/cas/uploads/";
$target = $target . basename( $_FILES['uploadedfile']['name']);

   if (isset($_POST['submit'])) {
   	  $id2 = mysql_real_escape_string(stripslashes($_POST["id2"]));
      $name = mysql_real_escape_string(stripslashes($_POST["name"]));
	  $email = mysql_real_escape_string(stripslashes($_POST["email"]));
	  $age = mysql_real_escape_string(stripslashes($_POST["age"]));
	  $birthday = mysql_real_escape_string(stripslashes($_POST["birthday"]));
	  $address = mysql_real_escape_string(stripslashes($_POST["address"]));
	  $number = mysql_real_escape_string(stripslashes($_POST["number"]));
	  $uploadedfile=($_FILES['uploadedfile']['name']);
 
	  $sql = "UPDATE contacts SET name='$name', email='$email', age='$age', birthday='$birthday', address='$address', number='$number', uploadedfile='$uploadedfile' WHERE id2=$id2";
 
      $result = mysql_query($sql) or die(mysql_error());
      echo "<div align=\"center\"><b>Thank you! Information updated.</b><br><br>";
	  echo "<a href=\"http://localhost/cas/contacts.php\">Contacts</a>"; echo " | ";
	  echo "<a href=\"http://localhost/cas/members.php\">Member Home</a>";echo " | ";
	  echo "<a href=\"http://localhost/cas/logout.php\">Logout</a></div>";echo "  ";
	  
	  //check image upload status
	  if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target))
{
echo "Thank you, " . $_SESSION["valid_user"];echo ". Not you? <a href=\"http://localhost/cas/logout.php\">Click here.</a>";
echo "<br>";
//Tells you if its all ok
echo "<br>";

echo "The file <b>". basename( $_FILES['uploadedfile']['name']). "</b> has been uploaded.";
echo "</div>";

}
else {

//Gives and error if its not
echo "<div align=\"center\">";
echo "Sorry, there was a problem uploading your file.";
echo "</div>";
}
	}
	
?>

Dani AI

Generated

Common pattern behind the symptoms in ’s post (no PHP error, database shows old filename, uploads folder empty) is not a single bug but a sequence: the form is not sent as multipart, the uploaded-file array is never checked, the server path or permissions prevent the move, or the DB is updated regardless of whether the file was actually saved. Also note the PHP mysql_* API used in the thread is deprecated — migrate to mysqli or PDO and use prepared statements.

Checklist of practical fixes and checks:

  • Ensure the HTML form uses enctype="multipart/form-data" and method="post". A file input cannot transmit without that.
  • Do not rely solely on isset($_POST['submit']); inspect $_FILES and check $_FILES['uploadedfile']['error'] === UPLOAD_ERR_OK'.
  • Build the destination path with DIR (or a configured upload dir), use basename() and sanitize the filename, and verify directory write permissions.
  • Call is_uploaded_file() and move_uploaded_file(), and only update the DB after move_uploaded_file() succeeds. If no new file was provided, leave the existing filename unchanged.
  • Verify php.ini settings: file_uploads = On, upload_max_filesize and post_max_size large enough, and inspect webserver/PHP error logs.
  • Cast numeric IDs to int and use prepared statements (PDO/mysqli) to avoid injection and future PHP incompatibility.

Compact example (illustrative flow — replace DSN/credentials and integrate into the full update logic):

if (isset($_FILES['uploadedfile']) && $_FILES['uploadedfile']['error'] === UPLOAD_ERR_OK) {
    $name = basename($_FILES['uploadedfile']['name']);
    $safe = time().'_'.preg_replace('/[^A-Za-z0-9._-]/','_',$name);
    $target = __DIR__.'/uploads/'.$safe;
    if (is_uploaded_file($_FILES['uploadedfile']['tmp_name']) && move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target)) {
        $pdo = new PDO($dsn,$user,$pass,$opts);
        $stmt = $pdo->prepare('UPDATE contacts SET uploadedfile = :f WHERE id2 = :id');
        $stmt->execute([':f'=>$safe, ':id'=>(int)$id2]);
    } else {
        // handle move error / log details
    }
} else {
    // no new file — update other fields only
}

Troubleshooting: enable error_reporting(E_ALL), var_dump($_FILES) to inspect the upload array, and tail the PHP/webserver error log. Applying the checklist above will resolve the common causes that produce the silent failure described in the original post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.