<td>Upload your pic</td>

      <td><?php if(!empty($picurl)){echo '<img src="'.$picurl.'" align="left"width="50px" height="50px"/>';}?>
      <input type="file" name="userfile"  ></td>
    </tr>
    <tr class="tableheader">
      <td colspan="4" class="last"><input name="submit" type="submit" class="button"  value="Update"/></td>
    </tr>
  </table>
</form>
<?php
if (isset($_POST["submit"])){

if($_FILES['userfile']['type']=="image/jpg" or
$_FILES['userfile']['type']=="image/jpeg" or
$_FILES['userfile']['type']=="image/gif" or
$_FILES['userfile']['type']=="image/png"){
echo "Error number: ".$_FILES['userfile']['error']."<br/>";
//$_Files is a global array,its 2 dimen
if(file_exists("image/".$_FILES['userfile']['name'])){
echo "file already exists in the folder";
}
else{
move_uploaded_file($_FILES['userfile']['tmp_name'],
"images/".$_FILES['userfile']['name']);
$uploaded_dir = "images/"; 
$filename = $_FILES["userfile"]["name"]; 
$path = $uploaded_dir . $filename;
}
}
}
if (isset($User_ID)){
$sql="UPDATE users SET PicUrl='$path' WHERE UserId='$User_ID'";
if (!mysql_query($sql)){
die('Error: '. mysql_error());
}
echo "Sucessfully Updated";
}

Dani AI

Generated

’s code updates the PicUrl unconditionally, so when no file is uploaded the DB can end up with a blank/undefined path. ’s tip to check for the file is on the right track, but the more reliable pattern is to check the upload error flag (UPLOAD_ERR_NO_FILE) and only run the UPDATE after a successful, validated upload. Below is a compact, safer pattern that (1) validates the upload, (2) generates a safe unique filename, and (3) updates the DB only when a new image was saved.

<?php
// assume $pdo is a PDO instance and $User_ID is set
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $newPath = null;

    if (isset($_FILES['userfile']) && $_FILES['userfile']['error'] !== UPLOAD_ERR_NO_FILE) {
        if ($_FILES['userfile']['error'] === UPLOAD_ERR_OK) {
            $tmp = $_FILES['userfile']['tmp_name'];

            // validate actual mime type
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mime = finfo_file($finfo, $tmp);
            finfo_close($finfo);

            $allowed = ['image/jpeg' => '.jpg', 'image/png' => '.png', 'image/gif' => '.gif'];
            if (isset($allowed[$mime]) && getimagesize($tmp)) {
                $filename = uniqid('img_') . $allowed[$mime];
                $destDir = __DIR__ . '/images/';
                if (!is_dir($destDir)) mkdir($destDir, 0755, true);
                if (move_uploaded_file($tmp, $destDir . $filename)) {
                    $newPath = 'images/' . $filename;
                }
            }
        }
        // handle other error codes (UPLOAD_ERR_INI_SIZE, etc.) as needed
    }

    if ($newPath !== null) {
        $stmt = $pdo->prepare('UPDATE users SET PicUrl = :pic WHERE UserId = :id');
        $stmt->execute([':pic' => $newPath, ':id' => $User_ID]);
    }
}
?>

Notes and cautions:

  • Check php.ini (upload_max_filesize, post_max_size) and the images folder permissions; silent failures often come from these.
  • Don’t trust $_FILES['type'] alone; use finfo/getimagesize. Limit file size and allowed types.
  • Remove or archive the old image only after the new file is stored successfully.
  • Use prepared statements (PDO/mysqli) instead of deprecated mysql_* functions and never use the original user filename directly.

Just test if (isset ($_FILES["userfile"]) ). Only run your code if that is true. You can replace this code with that of line 12.

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.