上传的图片没有显示在mysql表中PHP BLOB

时间:2015-07-24 10:40:39

标签: php

更新:我使用PHP BLOB上传了图像,但它们没有显示在mysql表中。图像正在移动到目标文件夹,但未显示在表格中。当我获取图像时,此代码无法显示

  

上传图片的代码

      <?php
        if (isset($_POST['add_p'])) {
            @$pn = $_POST['pname'];
            @$pp = $_POST['pprice'];
            @$pc = $_POST['pcategory'];
            @$date = date('d-m-y H:i:s');
            $img = $_FILES['img']['name'];
            $tmp_img = $_FILES['img']['tmp_name'];
            move_uploaded_file($tmp_img, "uploads/$img");
            $insert = "INSERT INTO products (`p_name`, `p_price`, `img`, `p_category`, `date_added`) VALUES ('$pn', '$pp', '$img', '$pc', '$date')";
            mysqli_query($con, $insert);
      }
    ?>
    <form action="add-product.php" method="POST" class="form">                                 
      Name: <input class="inpt" type="text" name="pname">
      Price: <input class="inpt" type="text" name="pprice">
      Upload Image: <input class="inpt" type="file" name="img">
      Select Category
            <select class="inpt" name="pcategory">
                <option>Men</option>
                <option>Women</option>
                <option>Kids</option>
            </select>
      <input type="submit" name="add_p" value="Add Product">
     </form>
  

获取图像的代码

     <?php 
        $get = "SELECT * FROM products ORDER BY p_id DESC LIMIT 0,4";
        $query = mysqli_query($con, $get);
        while ($row = mysqli_fetch_array($query)) {
            echo $id = $row['p_id'];
            echo $n = $row['p_name'];
            echo $p = $row['p_price'];
            echo $c = $row['p_category']; 
            echo $i = $row['img'];
        }
     ?>

1 个答案:

答案 0 :(得分:1)

在表单中添加enctype='multipart/form-data'以支持文件上传:

<form action="add-product.php" method="POST" class="form" enctype="multipart/form-data">

See this answer了解有关HTML表单编码类型的更多详细信息。