douang2297 2014-09-28 09:37
浏览 21
已采纳

使用图像更新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 2014-09-28 10:03
    关注

    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';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装