1 Replies - 829 Views - Last Post: 29 April 2009 - 09:14 AM

#1 [email protected]   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 310
  • Joined: 09-February 09

update record but not image file

Post icon  Posted 28 April 2009 - 01:36 PM

hello,

i originally had this in the php forum, but i really think it has to do more with my database... i want to allow a user to be able to update post_title and product_name without having to update the image. right now, if the user has an image, but chooses to just update the post_title and product_name, then the image is wiped out because my script looks at it as though they are updating no image.

any ideas? i know that i've got to do something about my updateSQL to separate it, but i don't know how to do this on the same page.

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));
	}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: update record but not image file

#2 xerxes333   User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 504
  • Joined: 05-July 07

Re: update record but not image file

Posted 29 April 2009 - 09:14 AM

You could do a variety of things to solve this problem but I think a good solution would be to pass a parameter when updating an existing record that tells the code to not change the image file.

if (!empty($filename) && !empty($_FILES['image_data']['type']) || !$filename){

//something quick and dirty
if($SOMETHING=='dontChangeImage'){
  $updateSQL = sprintf("UPDATE postingTable SET post_title=%s, product_name=%s WHERE post_id=%s",
					   GetSQLValueString($_POST['post_title'], "text"),
					   GetSQLValueString($_POST['product_name'], "text"),
					   GetSQLValueString($_POST['post_id'], "int"));

}
else{
  $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));
	}
}
}


This post has been edited by xerxes333: 29 April 2009 - 12:48 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1