I would appreciate if someone could help me with this issue. The problem is that i have a page where i upload an image and it´s description, but when i update the image description and keep the filefield blank when i press submit to update the description, my image disappears because i left the filefield blank, so it replaces my previous image for a blank.
Is there any way to update the description field and only update the image only if filefield is set?
Thanks in advance.
Heres my code:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
move_uploaded_file($_FILES['imagem']['tmp_name'],"../images/slide/".($_FILES['imagem']['name']));
$updateSQL = sprintf("UPDATE tab_imagens SET imagem=%s, titulo=%s, idUser=%s WHERE idImagem=%s",
GetSQLValueString("images/slide/".($_FILES['imagem']['name']), "text"),
GetSQLValueString($_POST['titulo'], "text"),
GetSQLValueString($_POST['idUser'], "text"),
GetSQLValueString($_POST['idImagem'], "int"));
mysql_select_db($database_ligar, $ligar);
$Result1 = mysql_query($updateSQL, $ligar) or die(mysql_error());
$updateGoTo = "verImagensSlide.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
Thank you all for your responses! This is how i solved it:
//do this if there is a file in filefield
if( !empty( $_FILES[ 'imagem' ] ) && !empty( $_FILES[ 'imagem' ][ 'tmp_name' ] )){
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
move_uploaded_file($_FILES['imagem']['tmp_name'],"../images/slide/".($_FILES['imagem']['name']));
$updateSQL = sprintf("UPDATE tab_imagens SET imagem=%s, titulo=%s, idUser=%s WHERE idImagem=%s",
GetSQLValueString("images/slide/".($_FILES['imagem']['name']), "text"),
GetSQLValueString($_POST['titulo'], "text"),
GetSQLValueString($_POST['idUser'], "text"),
GetSQLValueString($_POST['idImagem'], "int"));
mysql_select_db($database_ligar, $ligar);
$Result1 = mysql_query($updateSQL, $ligar) or die(mysql_error());
$updateGoTo = "verImagensSlide.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}}
//do this if there is NO FILE in filefield
else
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE tab_imagens SET titulo=%s, idUser=%s WHERE idImagem=%s",
GetSQLValueString($_POST['titulo'], "text"),
GetSQLValueString($_POST['idUser'], "text"),
GetSQLValueString($_POST['idImagem'], "int"));
mysql_select_db($database_ligar, $ligar);
$Result1 = mysql_query($updateSQL, $ligar) or die(mysql_error());
$updateGoTo = "verImagensSlide.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
if( !empty( $_FILES[ 'imagem' ] ) && !empty( $_FILES[ 'imagem' ][ 'tmp_name' ] )
move_uploaded_file($_FILES['imagem']['tmp_name'],"../images/slide/".($_FILES['imagem']['name']));
should do the trick.
Make sure you also change the SQL query by using the same if
-statement. It should update the image paths and names in the table