使用php无法正常将图像插入mysql

时间:2016-04-08 10:32:40

标签: php mysql mysqli blob blobstore

我的网站在mysqli数据库中插入名称,日期,消息和 IMG 等信息 这是我的代码

HTML& PHP代码

<form action='' method='POST'>
                <input type='date' name='bdate' min='2003-12-31' max='2016-04-02'><br><br> 
                  <input type='text' name='send_name'/>
                  <input type='text' name='min_name'  value='Some Name' disabled='true' ><br>   
                <textarea name='my_text' rows='11' cols='40'></textarea>
                      <input type='submit' name='submit' value='Send'>
                      <input type='file' name='Image' id='Image' >
                </form>

if (isset($_POST['submit'])) {
              $imgData = $_FILES['Image']['tmp_name'];
              $imageProperties = $_FILES['Image']['tmp_name'];
              $sql = "INSERT INTO output_images(imageType ,imageData)
              VALUES('".$imageProperties['mime']."', '".$imgData."')";
              $current_id = mysqli_query($connection,$sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error());
              if(isset($current_id)) {
                echo "Image Upload Ok";
                }
                $imgData = $_FILES['Image']['tmp_name'];
                $imageProperties = $_FILES['Image']['tmp_name'];
                $min_name = "Ministry Of Intinor";
                $dat = $_POST['bdate'];
                $info = $_POST['my_text'];
           $mySQL = mysqli_query($connection," INSERT INTO `mddb`.`ministry_tbl` (`sender_name`, `min_name`, `b_date`, `infos`,`img`,`img_name`) VALUES ('".$_SESSION['username']."', '".$min_name."', '".$dat."', '".$info."','".$imgData."','".$imageProperties['mime']."') ");
              if ($mySQL) {
                echo "Done";
              }
            }

我的问题是数据库没有存储* BLOB ** img及其总是[BLOB - 0 B]

注意 HERE IS MY DATABASE

2 个答案:

答案 0 :(得分:0)

you have to use enctype="multipart/form-data" add input type="file" before submit button

            <input type='date' name='bdate' min='2003-12-31' max='2016-04-02'><br><br>

              <input type='text' name='send_name'/>
              <input type='text' name='min_name'  value='Some Name' disabled='true' ><br>   
            <textarea name='my_text' rows='11' cols='40'>
                <input type='file' name='Image' id='Image' >
                  <input type='submit' name='submit' value='Send'>


            </form>

答案 1 :(得分:0)

更好的方法是不在数据库中保存图像,因为这不是一个好习惯。只需将上传的图像保存在目录中,并将该图像的路径保存在数据库中。它可以节省一些数据库空间和搜索时间。

供参考,请在PHP-SQL: Uploaded image displaying as junk text

查看我的答案