簡體   English   中英

如何使用PHP和MYSQL查看以BLOB格式存儲的圖像

[英]How to view images stored in BLOB format using PHP and MYSQL

我正在做一張注冊表,我也在其中將圖像保存到數據庫中,以及該圖像是以LONGBLOB格式存儲的。 我能夠添加並查看所有接受圖像部分的數據。

這是我的product_register.php代碼:

<form role="form" method="post" action="product_register.php">  
  <fieldset>
    <div class="form-group">  
      <input class="form-control" placeholder="Product Name" name="product_name" type="text" autofocus>  
    </div>  
    <div class="form-group">  
      <input class="form-control" placeholder="Product Code" name="product_code" type="text" autofocus>  
    </div>
    <div class="form-group">  
      <input class="form-control" placeholder="Product Image" name="product_image" type="file" value="">  
    </div> 
    <div class="form-group">  
      <input class="form-control" placeholder="Purchase Date" name="date" type="date" autofocus>  
    </div> 
    <div class="form-group">  
      <input class="form-control" placeholder="Time Guarantee" name="time_guarantee" type="text" autofocus>  
    </div>  

    <input class="btn btn-lg btn-success btn-block" type="submit" value="submit" name="submit" >
  </fieldset>  
</form>  
<?php  
   include("db_conection.php");//make connection here  
   //mysql_close();

   if(isset($_POST['submit']))  
    {
      $usern = $_SESSION['sess_username'];
      $product_name = $_POST['product_name'];
      $product_code = $_POST['product_code'];
      $product_image = $_POS['product_image'];
      $date = $_POST['date'];
      $time_guarantee = $_POST['time_guarantee'];
      $insert_user = "insert into product (product_name,product_code,product_image,date,time_guarantee,usern) VALUE ('$product_name','$product_code','$product_image','$date','$time_guarantee','$usern')";  
      if(mysqli_query($dbcon,$insert_user))  
       // $result = mysql_query($insert_user);  
       {  
          echo "<script type='text/javascript'>alert('Product added successfully !!!')</script>";
          // echo"<script>window.open('product_register.php','_self')</script>";  
          //  mysql_close();
       }  
   }  
?>

這是查看它的代碼view_product.php代碼:

 <table class="table table-bordered table-hover table-striped" style="table-layout: fixed">
    <thead>    
       <tr>
  <!--   <th>User Id</th>  -->
         <th>Product Name</th>
         <th>Product Code</th>
         <th>Product Image</th>
         <th>Date of Purchase</th>
         <th>Time Guarantee</th>
    <!-- <th>Delete User</th> -->
       </tr>
     </thead>

     <?php
        mysql_connect("localhost","root","") or die(mysql_error());
        mysql_select_db("gmgmt") or die(mysql_error());

        //  $view_users_query="select * from users WHERE role='user'";     //select query for viewing users.
        //  $view_users_query="select * from users WHERE role='admin'";     //select query for viewing users.
        $view_users_query="select * from 'product' WHERE 'usern' = 'demo'";     //select query for viewing users.
        //  $run=mysql_query($view_users_query);//here run the sql query.    // '".$_SESSION['userid']."'
        $check = mysql_query("SELECT * FROM product WHERE usern = '".$_SESSION['sess_username']."' ") or die(mysql_error());

        while($row=mysql_fetch_array($check))  //while look to fetch the result and store in a array $row.
        {
           $id=$row[0];
           $product_name=$row[1];
           $product_code=$row[2];
           $product_image=$row[3];
           $date=$row[4];
           $time_guarantee=$row[5];
        // $usern=$row[6];
        ?>
        <tr>
<!--here showing results in the table -->
  <!--     <td><?php echo $id;  ?></td>  -->
           <td><?php echo $product_name;  ?></td>
           <td><?php echo $product_code;  ?></td>
           <td><?php echo "<img src='php/imgView.php?imgId=".$product_image."' />";  ?></td>
           <td><?php echo $date;  ?></td>
           <td><?php echo $time_guarantee;  ?></td>
    <!--   <td><a href="delete.php?del=<?php echo $id ?>"><button class="btn btn-danger">Delete</button></a></td> -->  
        </tr>
        <?php } ?>
    </table>

我進行了很多搜索,但沒有找到適合我需要的代碼或方法。

echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['imageContent'] ) . '" />';

使用上面的代碼顯示您在mysql數據庫中的映像存儲。 希望這個能對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM