dps43633 2016-01-23 08:19
浏览 214
已采纳

如何使用php更新mysql中的longblob图像字段

 if(isset($_POST['submit']) and isset($_GET['slider_id']))
    {
        $date=date('Y-m-j');
        $imgName=$_FILES['image']['name'];
        $cont=file_get_contents($imgName);
         $cont=addslashes($cont);

         if($imgName=="")
         {
        //$imgData =addslashes(file_get_contents($_FILES['image']['name']));
        $res=mysqli_query($connect,'UPDATE `slider_images` SET `image`=\''.$cont.'\' WHERE id=\''.$_GET['slider_id'].'\'');
             if($res)
             {
                 echo "Updated";
             }
             else
             {
                 echo "Not Updated";
             }
        }
     }

Not understanding the real issue behind this and i have refereed many solution's but no success in that.All solution's i found they tell to store images in folder and store the file name in database table.Reason behind storing images in database is, only 4 images are to be stored, so why not to store them in database. Please guide me through this issue. Following is the issue i am talking about. Warning Message Thank's in advance.

  • 写回答

1条回答 默认 最新

  • drngnh708353 2016-01-23 08:35
    关注
    • $_FILES['file']['name'] is the original name of the uploaded file from the user's computer.
    • $_FILES['file']['tmp_name'] will contain the temporary file name of the file on the server. This is just a temporary placeholder until you process the file.

    So you should access the file like this:

    $cont=file_get_contents($_FILES['image']['tmp_name']);
    


    Sidenote: Instead of if($res){ ... } use mysqli_affected_rows() to get number of rows affected by this UPDATE query, like this:
    mysqli_query($connect,"UPDATE `slider_images` SET `image`='".$cont."' WHERE id='".$_GET['slider_id']."'");
    if(mysqli_affected_rows($connect)){
        echo "Updated";
    }else{
        echo "Not Updated";
    }
    

    Here's the reference:

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

报告相同问题?