【问题标题】:How to store and display image from a form using MYSQL ZEND1.12如何使用 MYSQL ZEND1.12 存储和显示表单中的图像
【发布时间】:2020-07-29 02:28:39
【问题描述】:

我正在使用 ZEND1.12,我想将图像从表单存储到数据库,然后将其显示在 CRUD 表上。 这是表单的代码:Product.php

<?php

类 Application_Form_Product 扩展 Zend_Form {

受保护的 $db;

public function __construct($db)
{
    $this->db = $db;

    // Don't forget to call the parent __construct. Ultimately
    // it is the parent __construct() that calls your init()
    // method that adds your elements 
    parent::__construct();
}

public function init()
{

    $this->addElement('select', 'category', array('label'=>'Category', 'multiOptions' => $this->buildMultiOptions()));

     $this->addElement('text', 'name', array('label' => 'Enter The Name:','required' => true));

     $this->addElement('select', 'warehouse', array('label'=>'Warehouse', 'multiOptions' => $this->buildMultiOptionsWarehouse()));
     $this->addElement('file', 'picture', array('label' => 'Enter The picture:','required' => true));
     $this->addElement('text', 'price', array('label' => 'Enter The Price:','required' => true));
   //  $this->addElement('file','file',array('label' => 'Add file'));

     $this->addElement('submit','submit',array('label' => 'Add Product'));
}

} 这是具有所有操作的控制器:

<?php

类 ProductController 扩展 Zend_Controller_Action {

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{
      $categoryModel = new Application_Model_DbTable_Category();
      $warehouseModel = new Application_Model_DbTable_Warehouse();
      $productModel = new Application_Model_DbTable_Product(); 
      $products = $productModel->showProducts();


      $this->view->products = $products;
}

public function createAction()
{
    $db = $this->getInvokeArg('bootstrap')->getResource('db');
    $productForm = new Application_Form_Product($db);
       $request = $this->getRequest();
        if ($request->isPost()) {
             if ($productForm->isValid($request->getPost())) {
                 $productModel = new Application_Model_DbTable_Product();
                 $productModel->create();
                 $this->_redirect('product');
            }
                                }
             $this->view->productForm= $productForm;

}

public function deleteAction()
{
      $productModel = new Application_Model_DbTable_Product();
      $productModel->deleteProduct();
      $this->_redirect('product');
}

public function editAction()
{
      $db = $this->getInvokeArg('bootstrap')->getResource('db');
      $productForm = new Application_Form_Product($db);
      $request = $this->getRequest();
      $id = $request->getParam('id');
      $productModel = new Application_Model_DbTable_Product();
      $product = $productModel->fetchRow('id = '.$id);

      if ($request->isPost()) {
           $productModel->edit();
           $this->_redirect('product');
      }
      $this->view->product = $product;
      $this->view->productForm = $productForm;
}

} 这是视图:

</br>
 </br>
      <table class="table">
      <thead class="thead-dark">
        <tr>
          <th scope="col" class="mx-auto" style="width: 300px;">Name</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Category</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Warehouse</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Picture</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Price</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Action</th>
        </tr>
      </thead>
     <?php 
          foreach ($this->products as $product) 
          {
                echo "<tr>";
                echo "<td>" . $product->name_product . "</td>";
                echo "<td>" . $product->name . "</td>";
                echo "<td>" . $product->warehouse_id . "</td>";
                echo "<td>" . $product->picture . "</td>";
                echo "<td>" . $product->price . "</td>";
                echo "<td colspan='2'><a href='" . $this->url(array('controller' => 'product', 'action' => 'edit', 'id' => 
                $product->id)) . "' type='button' class='btn btn-primary'>Edit</a>";
                echo " <a href='" . $this->url(array('controller' => 'product', 'action' => 'delete', 'id' => $product->id)) . "' onclick='return confirm(\"Do you really want to delete this contact?\");' type='button' class='btn btn-danger'>Delete</a></td>";
                echo "</tr>";
         }
   ?>
   </table>

这是模型:DbTable : Product.php 具有所有功能。

<?php

类 Application_Model_DbTable_Product 扩展 Zend_Db_Table_Abstract {

protected $_name = 'products';

public function create() {
      $front = Zend_Controller_Front::getInstance();
      $request = $front->getRequest();
      $data = array(
           'id' => $request->getPost('id'),
           'name_product' => $request->getPost('name'),
           'category_id' => $request->getPost('category'),
           'warehouse_id' => $request->getPost('warehouse'),
           'picture' => $request->getPost('picture'),
           'price' => $request->getPost('price')

      );
      $this->insert($data);
 }

  public function edit() {
      $front = Zend_Controller_Front::getInstance();
      $request = $front->getRequest();
      $data = array(
           'name_product' => $request->getPost('name'),
           'category_id' => $request->getPost('category'),
           'warehouse_id' => $request->getPost('warehouse'),
           'picture' => $request->getPost('picture'),
           'price' => $request->getPost('price'),
      );
      $where = array('id = ?' => $request->getParam("id"));
      $this->update($data, $where);
 }

 public function deleteProduct() {
      $front = Zend_Controller_Front::getInstance();
      $request = $front->getRequest();
      $where = array('id = ?' => $request->getParam("id"));
      $this->delete($where);
 }

 public function showProducts() {
      $select = $this->select()
               ->from('products')
               ->setIntegrityCheck(false)
               ->join('categorys', 'products.category_id =categorys.id', array('*'))
               ->columns(array('products.id'));
       $result = $this->fetchAll($select);  

if ($result !== null){

    return $result;
} else {
    echo "no records found";
}   
 }

 public function showProductsWarehouse() {
      $select = $this->select()
               ->from('products')
               ->setIntegrityCheck(false)
               ->join('warehouses', 'products.warehouse_id =warehouses.id', array('*'))
               ->columns(array('products.id'));
       $result = $this->fetchAll($select);  

if ($result !== null){

    return $result;
} else {
    echo "no records found";
}   
 }

}

【问题讨论】:

  • 您的问题中似乎没有出现产品型号。
  • 是的,确实是我编辑的,你可以看看,谢谢。
  • 不客气。这似乎您更喜欢将帖子图像直接保存到数据库,因此这可能是 Web 服务器和数据库服务器的开销。 1. 将文件放入数组。 2. 通过数据库连接协议存储到数据库中。当在客户端检索视图时。 1. 通过协议从数据库中检索作为查询结果。 2. 作为列模型存储到数组中,并由控制器用于准备和呈现给客户端。但是根据文件大小,如果只有 1-2kb,它仍然是有意义的,但是它需要在存​​储到数据库之前通过适当的图像验证。
  • 对不起,我不明白,你能更具体地说明我应该在代码中具体做什么来存储和显示图像吗?
  • 谷歌“mysql php blob 示例”,你会发现大量的帮助。而这个告诉显示来自数据库的图像:stackoverflow.com/questions/23842268/…

标签: php mysql database zend-framework crud


【解决方案1】:

使用 getFiles 和 file_get_contents 获取持久 blob 列的图像文件:

$rowData = [
       'name_product' => $request->getPost('name'),
       'category_id' => $request->getPost('category'),
       'warehouse_id' => $request->getPost('warehouse'),
       'picture' => file_get_contents($request->getFiles('picture')->toArray()['tmp_name']),
       'price' => $request->getPost('price')
];

getFiles 参考:https://docs.zendframework.com/zend-http/request/

直接显示来自持久 blob 的图像文件。

jpg图片:

echo '<td><img src="data:image/jpeg;base64,'.base64_encode( $product->picture ).'"/></td>';

png图片:

echo '<td><img src="data:image/png;base64,'.base64_encode( $product->picture ).'"/></td>';

---==^^^==---

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 2012-10-24
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2015-09-23
    • 2018-11-21
    相关资源
    最近更新 更多