update Record w/o updating image file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

update Record w/o updating image file

Post 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));
    }
}
}
 
Last edited by Benjamin on Mon Apr 27, 2009 8:14 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: update Record w/o updating image file

Post by Christopher »

Only add the columns you want to update to the UPDATE statement.
(#10850)
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: update Record w/o updating image file

Post 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(...)
?>
 
Last edited by Benjamin on Mon Apr 27, 2009 10:28 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply