2017-08-07 185 views
1

值正在存储在数据库中,但它不是我所期待的。我以前尝试过很多方法,但这种似乎可行,但文件名不存储,当我试图直接从数据库下载文件时,它下载一个.bin文件格式,它看起来像table_Name-column_Name.bin 。存储文件名是BLOB - ## B.图片没有正确存储在MySQL数据库里

我的形式

<form class="form-horizontal" method="post" action="productsValidate.php" name="myForm" enctype="multipart/form-data"> 
    <fieldset> 
     <legend>Add Product</legend> 
     <div class="form-group"> 
     <label for="Product_Name" class="col-lg-2 control-label">Product Name</label> 
     <div class="col-lg-10"> 
      <input type="text" class="form-control" id="Product_Name" placeholder="Name" required="required" name="Product_Name"> 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="Size" class="col-lg-2 control-label">Size</label> 
     <div class="col-lg-10"> 
      <input type="text" class="form-control" id="Size" placeholder="Size" required="required" name="Size"> 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="Color" class="col-lg-2 control-label">Color</label> 
     <div class="col-lg-10"> 
      <input type="text" class="form-control" id="Color" placeholder="Size" required="required" name="Color"> 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="price" class="col-lg-2 control-label">Price</label> 
     <div class="col-lg-10"> 
      <input type="number" class="form-control" id="price" placeholder="price" required="required" name="price"> 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="image" class="col-lg-2 control-label">Select Image</label> 
     <div class="col-lg-10"> 
      <input type="file" name="image" id="image"> 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="categoryId" class="col-lg-2 control-label">Category Id</label> 
     <div class="col-lg-10"> 
      <?php 
       //your connection to the db and query would go here 
       include "../include/settings.php"; 
       $conn = new mysqli($host, $user, $pwd, $sql_db); 
       if ($conn->connect_error) { 
        die("Connection failed: " . $conn->connect_error); 
       } 
       $sql = "SELECT distinct Category_Id FROM products"; 
       $result = mysqli_query($conn, $sql); 
       ?> 
      <select id="categoryId" name="categoryId"> 
       <option value = ""></option> 
       <?php 
        while($row = mysqli_fetch_array($result)) { 
        echo '<option value='.$row['Category_Id'].'>'.$row['Category_Id'].'</option>'; 
        } 
        ?> 
      </select> 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="description" class="col-lg-2 control-label">Description</label> 
     <div class="col-lg-10"> 
      <textarea type="text" class="form-control" id="description" placeholder="Description" required="required" name="description" pattern="[\sA-Za-z]+"></textarea> 
     </div> 
     </div> 
     <div class="form-group"> 
     <div class="col-lg-6 col-lg-offset-2"> 
      <button type="submit" class="btn btn-primary">Add Product</button> 
     </div> 
     </div> 
    </fieldset> 
</form> 

我的表单验证

<?php 

    $name = $_POST["Product_Name"]; 
    $size = $_POST["Size"]; 
    $color = $_POST["Color"]; 
    $price = $_POST["price"]; 

    $image = addslashes($_FILES['image']['tmp_name']); 
    $image = file_get_contents($image); 
    $image = base64_encode($image); 
    $image=basename($_FILES["image"]["tmp_name"],".jpg"); 

    $category = $_POST['categoryId']; 
    $description = $_POST['description']; 
    insertProduct($name, $size, $color, $price, $image, $category, $description); 

    function insertProduct($name, $size, $color, $price, $image, $category, $description){ 

     require_once ("../include/settings.php"); // Load MySQL log in credentials 
     $conn = @mysqli_connect ($host,$user,$pwd,$sql_db); // Log in and use database 
     if ($conn) { // check is database is avialable for use 
      $query = "INSERT INTO products 
            (Product_Id, Product_Name, Size, Color, Price, Picture, Category_Id, Description) 
           VALUES ('', '$name', '$size', '$color', '$price', '$image', '$category', '$description')"; 

      $result = mysqli_query ($conn, $query); 
      if ($result) {        // check if query was successfully executed 
       echo 'Successfully Added'; 
      } else { 
       echo 'Product could not be added'; 
      } 
      mysqli_close ($conn);     // Close the database connect 
     } else { 
      echo "<p>Unable to connect to our database for adding the product.</p>"; 
     } 
    } 
?> 

Database

+0

将图片上传到服务器上的文件夹并将其路径存储在数据库中通常会更好。 – Qirel

回答

2

我想你想将实际编码的图像存储在数据库中,而不是指向它的指针。它看起来像你的11个字节的BLOB中有指针。

你的代码包含这一系列的行。

$image = addslashes($_FILES['image']['tmp_name']); 
$image = file_get_contents($image); 
$image = base64_encode($image); 
$image=basename($_FILES["image"]["tmp_name"],".jpg"); 

第三行将图像的编码而非二进制版本放入文本字​​符串中。这与您想要的接近,但如果您要放入BLOB,则可能不应该对它进行64位编码。

第四行放弃图像本身并用图像名称覆盖图像。我认为这是错误的。

如果您打算使用BLOB数据这种方式,您还需要使用mysqli的工具来prepare您的SQL语句,然后bind您的参数。 bind_param()函数为您提供了一种声明参数为blob的方法。这比试图欺骗php的字符串处理接受它更好。

就这么说,大多数人使用文件系统或内容服务器而不是BLOB来存储图像并将其提供给Web客户端。 BLOB编程是一个痛苦的脖子。另外,使用DBMS快速存储和检索图像成为扩展应用程序中的性能瓶颈。