dps43633
dps43633
2016-01-23 08:19

如何使用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 drngnh708353 5年前
    • $_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:

    点赞 评论 复制链接分享