Mysql更新查询以更改或保持上传的图像

时间:2016-03-17 22:05:00

标签: php mysql

通过练习,我正在建立自己的博客。只是为了了解PHP,MySQL和那里的结构。一切顺利,但遇到UPDATE问题。让我们说我创建了一个包含标题,内容和图像的帖子。现在,当我转到页面编辑我的帖子时,我改变了我的标题(不涉及内容或图像)。运行更新查询后,我之前上传的图像消失了。如果我编辑帖子并上传新图像一切都很好。我希望这说清楚(不太好用行话......)所以,在编辑时,如果我没有上传新图像,当更新帖子时当前图像会消失。这是代码:

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>

2 个答案:

答案 0 :(得分:1)

原因是在EditPost部分您只需采用post_image文件上传控件中的任何内容即可。如果您没有为帖子选择新文件,则此控件将不会包含任何内容,因此它将使用空字符串覆盖现有的post_image值。

如果帖子必须包含图片,请检查EditPost部分中post_image是否包含内容,如果没有,请将其从更新语句中删除。

如果您不需要帖子来获取图片,那么请单独选中“删除现有图片”复选框?如果用户检查它,则将post_image设置为空字符串并删除图像文件。如果未选中此复选框,但post_image控件保留为空,则不要将db中的post_image字段设置为空字符串。

答案 1 :(得分:0)

感谢@shadow我明白了。我更改了更新查询的条件。这是我的解决方案:

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