I'm trying to update content in a real simple content management system. The idea being that a user can log in and amend content.
The problem I am having is if the user logs in, changes content in the news section, but does not change an image, upon saving the image file is over written with nothing. How do I make the content update but if a user does not change the image, it leaves this section alone and doesn't over write the image.
I had previously tried saving the old image file name and using this instead of, but for some reason it always seems to use a new file image name even if this is blank.
I guess I need something like:
if(image is not changed){
$filename = uploaded image;
}else{
$filename = old file name;
I just don't know how to do the first if statement, it seems to always detect the change of image even if the change is NULL
or some kind of nothing.
I've written the below code to hopefully make sense of it.
if (isset($_POST['update'])) {
$e_id = sanitizestring($_POST['edit_id']);
$title = sanitizestring($_POST['newstitle']);
$date = sanitizestring($_POST['newsdate']);
$content = sanitizestring($_POST['newscontent']);;
if ($_FILES['news_img']['error'] == 0) {
move_uploaded_file($_FILES['news_img']['tmp_name'], "uploads/news/".$_FILES['news_img']['name']);
$filename = $_FILES['news_img']['name'];
}
$edit_q ='
UPDATE
`news`
SET
`news_title` = ?,
`news_date` = ?,
`news_article` = ?,
`news_img` =?
WHERE
`news_id` = ?
';
$edit_stmt = $pdo->prepare($edit_q);
if($edit_stmt -> execute(array($title, $date, $content, $link, $linktext, $filename, $e_id))){
$successMsg = 'Edit successful';
}else{
$failMsg = 'Unable to edit as this time';
echo $failMsg;
}// end if edit successful
}// end if update is set