AdRock Posted August 26, 2006 Share Posted August 26, 2006 I am trying to update the database but have come across a problem. The problem lies with the second else statement but how do i fix it?I have form where I can make changes to the record and one of the fields is to upload an image. Sometimes I don't want to upload an image and keep the one that's already in the database. This is where the error occurs.Before, if i didn't select an image it would update the image name as blank so when called no image would be displayed.What i want is if i don't upload an image the imge name stays the same.Here is the code i tried but doesn't work[code]include_once("../includes/connection.php"); if(empty($pic)) { $query="UPDATE news SET title='$name', content='$message' WHERE id='$ud_id'"; mysql_query($query); } else { $query="UPDATE news SET title='$name', content='$message', photo='$pic' WHERE id='$ud_id'"; mysql_query($query); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } mysql_close();[/code] Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 26, 2006 Share Posted August 26, 2006 ifelseifelse Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted August 26, 2006 Share Posted August 26, 2006 Why don't you just not update that part on the second else, like so:[code]include_once("../includes/connection.php"); if(empty($pic)) { $query="UPDATE news SET title='$name', content='$message' WHERE id='$ud_id'"; mysql_query($query); } elseif { $query="UPDATE news SET title='$name', content='$message', WHERE id='$ud_id'"; mysql_query($query); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } mysql_close();[/code]and as AndyB said. I added that to this code. Quote Link to comment Share on other sites More sharing options...
AdRock Posted August 26, 2006 Author Share Posted August 26, 2006 i tried that but it didn't workI used this which now does work[code] include_once("../includes/connection.php"); if(empty($pic)) { $query="UPDATE news SET title='$name', content='$message' WHERE id='$ud_id'"; mysql_query($query); } else { $query="UPDATE news SET title='$name', content='$message', photo='$pic' WHERE id='$ud_id'"; mysql_query($query); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } mysql_close();[/code] Quote Link to comment Share on other sites More sharing options...
AdRock Posted August 26, 2006 Author Share Posted August 26, 2006 I do have a similar problem now though.Not only sometimes do I not what to update the image but I also don't want to change a date that's in the databaseWhere i have this [b]list($day, $month, $year) = explode("-", $event);[/b] when i don't change the date, it goes crazy and makes a weird date in thge future and i know why.....i just need to be able to ignore either the image, the date or both[code] include_once("../includes/connection.php"); mysql_connect($host,$user,$password); @mysql_select_db($database) or die( "Unable to select database"); list($day, $month, $year) = explode("-", $event); $EventDate = date('Y-m-d', strtotime("$year/$month/$day")); $query="UPDATE events SET title='$name', eventdate='$EventDate', content='$message', photo='$pic' WHERE id='$ud_id'"; mysql_query($query); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } mysql_close();[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.