duanpai1920 2013-01-23 12:34
浏览 11
已采纳

在php中更新记录后,图像应保持不变

i am uploading some data to the db that contain title, news date and image, but when i want to update it i am facing problems.what i want to do basically if i update all the row except image then updation do not take place. what i want basically when i have to update lets suppose title only then it should update it but all other data should remain same but problem is that i have to select image again from my pc in updation. my scenario is that i just save the name in db not the whole path and hard code the path where ever i need to display the image

here is html

 <div class="row">
                            <label>Image upload</label>
                            <div class="right"><input type="file" name="file" value="<?php echo $row['images'];?>" /></div>
                        </div>

here is php

   if(($_GET['mod']=='edit') && (isset($_POST['hidden'])))
{




    echo $_FILES["file"]["name"];
        $allowedExts = array("jpg", "jpeg", "gif", "png");
        $extension = end(explode(".", $images));
        if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"]   == "image/jpeg")
        || ($_FILES["file"]["type"]   == "image/png")
        || ($_FILES["file"]["type"]   == "image/pjpeg"))

        && in_array($extension, $allowedExts))
          {
           if ($_FILES["file"]["error"] > 0)
            {

            echo $_FILES["file"]["error"] . "<br>";
            }
          else
            {
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload-images/" . $images);
            $update="UPDATE headline SET 
                                                headline_title  = '$title',   
                                                headline_des    = '$description',   
                                                month           = '$month_name', 
                                                day             = '$day_name', 
                                                year            = '$year_name', 
                                                featured        = '$featured',
                                                headline        = '$headline',
                                                images          = '$images' 
                                                where id        = ".$_GET['id']."";
             $result = mysql_query($update);    

              }
            }
  • 写回答

1条回答 默认 最新

  • dongliu8559 2013-01-23 12:42
    关注

    Submitting the form again will cause the new value of the file input which will be empty so you have to check if it's empty or not and act according to the status

    For example

    if($_FILES['file']['name'] != "")
    {
          //upload the image with what ever you want 
          //move_uploaded_file($_FILES['file']['tmp_name'],$target.$image );
          //the SQL query should contain the updating the image column 
             if(($_GET['mod']=='edit') && (isset($_POST['hidden'])))
             {
    
    
    
    
    echo $_FILES["file"]["name"];
        $allowedExts = array("jpg", "jpeg", "gif", "png");
        $extension = end(explode(".", $images));
        if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"]   == "image/jpeg")
        || ($_FILES["file"]["type"]   == "image/png")
        || ($_FILES["file"]["type"]   == "image/pjpeg"))
    
        && in_array($extension, $allowedExts))
          {
           if ($_FILES["file"]["error"] > 0)
            {
    
            echo $_FILES["file"]["error"] . "<br>";
            }
          else
            {
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload-images/" . $images);
            $update="UPDATE headline SET 
                                                headline_title  = '$title',   
                                                headline_des    = '$description',   
                                                month           = '$month_name', 
                                                day             = '$day_name', 
                                                year            = '$year_name', 
                                                featured        = '$featured',
                                                headline        = '$headline',
                                                images          = '$images' 
                                                where id        = ".$_GET['id']."";
             $result = mysql_query($update);    
    
              }
            }
    }
    else
    {
          //SQL update without image 
            $update="UPDATE headline SET 
                                                headline_title  = '$title',   
                                                headline_des    = '$description',   
                                                month           = '$month_name', 
                                                day             = '$day_name', 
                                                year            = '$year_name', 
                                                featured        = '$featured',
                                                headline        = '$headline'
                                                WHERE id        = ".$_GET['id']."";
             $result = mysql_query($update);    
    
    }
    

    if you don't want the image to be edited simply you can remove it from the update and the form used for that or but it's path in hidden input

    Hope this will help

    According to your comment the query should look something like this

            $update="UPDATE headline SET 
                                                headline_title  = '$title',   
                                                headline_des    = '$description',   
                                                month           = '$month_name', 
                                                day             = '$day_name', 
                                                year            = '$year_name', 
                                                featured        = '$featured',
                                                headline        = '$headline'
                                                WHERE id        = ".$_GET['id']."";
             $result = mysql_query($update);    
    

    I think you need to use good intending to make your code more readable and maintainable in the future :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用