douang2297
douang2297
2014-09-28 09:37

使用图像更新Mysql内容

已采纳

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 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doufei2662 doufei2662 7年前

    Your problem is that $filename is empty when you have no image uplaoded and file name set to the var. So you can add one more if statement before:

    if($edit_stmt -> execute(array($title, $date, $content, $link, $linktext, $filename, $e_id))){
        $successMsg = 'Edit successful';
    }else{
        $failMsg = 'Unable to edit as this time';
    

    Where you should prepare a statement without the filename:

    if ($empty($filename))
    {
     if($edit_stmt -> execute(array($title, $date, $content, $link, $linktext, $filename, $e_id))){
        $successMsg = 'Edit successful';
    }else{
        $failMsg = 'Unable to edit as this time';
    }
    else
    {
      if($edit_stmt_no_filename -> execute(array($title, $date, $content, $link, $linktext, $e_id))){
        $successMsg = 'Edit successful';
    }else{
        $failMsg = 'Unable to edit as this time';
    }
    
    点赞 评论 复制链接分享
  • dounie5475 dounie5475 7年前

    The answer in extremely simple: you should not change news_img if there is no new uploaded file:

    if($filename)
    {
    $edit_q ='
        UPDATE
        `news`
        SET
        `news_title` = ?,
        `news_date` = ?,
        `news_article` = ?,
        `news_img` =?
        WHERE
        `news_id` = ?
    ';
    }
    else
    {
    $edit_q ='
        UPDATE
        `news`
        SET
        `news_title` = ?,
        `news_date` = ?,
        `news_article` = ?
        WHERE
        `news_id` = ?
    ';
    }
    

    of course you'll need to move execute statement inside above if block & provide suitable number of arguments for it.

    点赞 评论 复制链接分享