[英]Mysql Update query to change or keep uploaded image
通過練習,我正在建立自己的博客。 只是要了解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>
這樣做的原因是,在EditPost
部分中,您只需采用post_image
文件上載控件中的任何內容。 如果您沒有為帖子選擇新文件,則此控件將沒有任何內容,因此它將用空字符串覆蓋現有的post_image
值。
如果帖子必須具有圖像,則在EditPost
部分中檢查EditPost
是否post_image
具有內容,如果沒有,則將其從update語句中刪除。
如果您不要求帖子具有圖像,則有一個單獨的復選框顯示“刪除現有圖像”? 如果用戶post_image
它,則將post_image
設置為空字符串,並同時刪除該圖像文件。 如果未選中此復選框,但post_image
控件保留為空,則不post_image
db中的post_image
字段設置為空字符串。
感謝@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));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.