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条)

报告相同问题?

悬赏问题

  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改