Bas Schreuder
Bas Schreuder

Reputation: 172

Mysql Update query to change or keep uploaded image

By way of practicing I am building my own blog. Just to learn about PHP, MySQL and there structure. All goes well, but am running into an UPDATE problem. Let's say I have created a post with title, content and an image. Now, when I go to the page to edit my post I change my title ( not touching the content or image ). After running the update query my image that I uploaded before is gone. If I edit the post and upload a new image all is fine. I hope this makes it clear ( not so good at the jargon.. ) So, when editing, if i do not upload a new image the current image dissapears when updating the post. Here is the code:

if(isset($_FILES['post_image'])){

    $errors= array();
    $file_name = $_FILES['post_image']['name'];
    $file_size =$_FILES['post_image']['size'];
    $file_tmp =$_FILES['post_image']['tmp_name'];
    $file_type=$_FILES['post_image']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['post_image']['name'])));

    $expensions= array("jpeg","jpg","png");         
    if(in_array($file_ext,$expensions)=== false){
        $errors[]="extension not allowed, please choose a JPEG or PNG file.";
    }
    if($file_size > 2097152){
    $errors[]='File size must be excately 2 MB';
    }               
    if(empty($errors)==true){
        move_uploaded_file($file_tmp,"post_images/".$file_name);
        echo "Success";
    }else{
        print_r($errors);
    }
}





if(isset($_POST['EditPost'])) {


$post_id=$_GET['id'];
$post_title = $_POST['post_title'];
$post_content = $_POST['post_content'];
$post_cat_id = $_POST['post_cat_id'];
$post_tags = $_POST['post_tags'];
$post_template = $_POST['post_template'];
$post_image  = $_FILES['post_image']['name'];





$sql = "UPDATE posts SET post_template=?, post_title=?, post_content=?,     post_tags=?, post_image=?, post_cat_id=? WHERE post_id=?";
$query = $db->prepare($sql);
$query->execute(array($post_template,$post_title,$post_content,$post_tags,$post_image,$post_cat_id,$post_id));

$succes_update = '<div class="alert alert-success" role="alert">Uw post is ge-update!</div>';;
}

    $id=$_GET['id'];
$result = $db->prepare("SELECT posts.post_id, posts.post_template, posts.post_title, posts.post_content, posts.post_tags, posts.post_image, posts.post_cat_id, categories.cat_id, categories.cat_title FROM posts INNER JOIN categories ON posts.post_cat_id=categories.cat_id WHERE post_id= :userid ");      
$result->bindParam(':userid', $id);
$result->execute();
    $row = $result->fetch(PDO::FETCH_ASSOC);

?>



<div class="col-md-8">

<h2>Edit this Post</h2>
<?php 
if(isset($succes_update)){
echo $succes_update; 
}?>
<form action="" method="POST" enctype="multipart/form-data">

Post Title<br>
<input type="text" class="form-control" name="post_title" value="<?php echo     $row['post_title']; ?>"><br>
Post Content<br>
<textarea  style="width:100%; height:200px" class="form-control"     n name="post_content"><?php echo $row['post_content']; ?></textarea><br>
Post Image<br>
<?php if(!empty($row['post_image'])) {?><img width="400px"       src="post_images/<?php echo $row['post_image']; ?>"/><?php } else { echo "        <em>There is no image set here</em>"; } ?><br><br>
<input type="file" name="post_image" /><br><br>
Post Template<br>
<input type="text" class="form-control" name="post_template" value="<?php echo  $row['post_template']; ?>"><br>
<br>
Post Tags - (comma seperated)<br>
<input type="text" class="form-control" name="post_tags" value="<?php echo       $row['post_tags']; ?>"><br>
 Post Category<br>
<select name="post_cat_id">

<option selected><?php echo $row['cat_id']; ?> = <?php echo $row['cat_title']; ?></option>
<?php
$result = $db->prepare("SELECT DISTINCT cat_id, cat_title FROM categories     WHERE parent_id > 0");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){   
echo "<option>". $row['cat_id'] . " = " . $row['cat_title'] . "</option>"; 
}
?>

</select><br><br>




<input type="submit" class="btn btn-primary" value="Edit this Post" name="EditPost" />
</form>

Upvotes: 0

Views: 1723

Answers (2)

Bas Schreuder
Bas Schreuder

Reputation: 172

Thanks to @shadow I figured it out. I changed the condition for the update query. This is my solution:

if($post_image!= null AND isset($_FILES['post_image'])){

$sql = "UPDATE posts SET post_template=?, post_title=?, post_content=?,    post_tags=?, post_image=?, post_cat_id=?, post_video_url=? WHERE post_id=?";
$query = $db->prepare($sql);
$query->execute(array($post_template,$post_title,$post_content,$post_tags,$post_image,$post_cat_id,$post_video_url,$post_id));
}
else {
$sql = "UPDATE posts SET post_template=?, post_title=?, post_content=?, post_tags=?, post_cat_id=?, post_video_url=? WHERE post_id=?";
$query = $db->prepare($sql);
$query->execute(array($post_template,$post_title,$post_content,$post_tags,$post_cat_id,$post_video_url,$post_id));
}

Upvotes: 0

Shadow
Shadow

Reputation: 34232

The reason for this is that in the EditPost section you simply take whatever is in the post_image file upload control. If you did not select a new file for the post, then this control will not have any content, thus it will override the existing post_image value with an empty string.

If a post must have an image, then check in the EditPost section if post_image has content at all and if not, then leave it out from the update statement.

If you do not require a post to have an image, then have a separate checkbox that says "delete exsisting image"? If the user checks it, then set the post_image to an empty string and remove the image file as well. If this checkbox is unchecked, but the post_image control was left empty, then do not set post_image field in the db to empty string.

Upvotes: 1

Related Questions