Page 1 of 1

update Record w/o updating image file

Posted: Mon Apr 27, 2009 5:03 pm
by ninethousandfeet
hello,

i have an update form where a user can update their post (which includes a posting title, product name, and optional image file).
my problem is that if a user wants to update for example just their posting title and product name, but leave the current image alone, they can't do it. what happens is the posting title and product name would be updated successfully, but the image(no-image, because they didn't attach a new image) is updated to no image.
... so now that post is updated with a new posting title, new product name, and no pic.

how can i stop the pic from being overwritten in the case where the user only wants to update other info in the form?

thank you!

Code: Select all

 
if(!$error) {
$username = $_SESSION['MM_Username'];
ini_set('date.timezone','America/Los Angeles');
$now = date('Y-m-d-His');
$emptyOK = false;
if (empty($filename) && empty($_FILES['image_data']['type'])) {
    $emptyOK = true;
}
if (!empty($filename) && !empty($_FILES['image_data']['type']) || !$filename){
 
  $updateSQL = sprintf("UPDATE postingTable SET post_title=%s, product_name=%s, image_data=%s, image_type=%s WHERE post_id=%s",
                       GetSQLValueString($_POST['post_title'], "text"),
                       GetSQLValueString($_POST['product_name'], "text"),
                       GetSQLValueString($filename, "text"),
                       GetSQLValueString($_FILES['image_data']['type'], "text"),
                       GetSQLValueString($_POST['post_id'], "int"));
 
  mysql_select_db($database_connUser, $connUser);
  $Result1 = mysql_query($updateSQL, $connUser) or die(mysql_error());
 
  $updateGoTo = "USERprofile.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
if (!empty($_FILES['image_data']['name'])) {
if (!is_dir("$uploadDIR/$username")) {
    mkdir("$uploadDIR/$username", 0777, true);
    }
if (!file_exists("$uploadDIR/$username/$filename")) {
move_uploaded_file($fileTemp, "$uploadDIR/$username/$filename");
} else {
    mkdir("$uploadDIR/$username/$now");
    move_uploaded_file($fileTemp, "$uploadDIR/$username/$now/$filename");
  }
}
  header(sprintf("Location: %s", $updateGoTo));
    }
}
}
 

Re: update Record w/o updating image file

Posted: Mon Apr 27, 2009 7:21 pm
by Christopher
Only add the columns you want to update to the UPDATE statement.

Re: update Record w/o updating image file

Posted: Mon Apr 27, 2009 9:56 pm
by ninethousandfeet
so can i do some type of conditional statement and have two update SQL?

something like:

Code: Select all

 
if (all fields not empty) { 
 updateSQL everything;
 upload files to server... etc...
} else {
if (image field empty) {
 update only the post and product;
}
 
header(...)
?>