简体   繁体   English

Codeigniter - 如何设置从数据库填充数据的选定选项的值

[英]Codeigniter - How to Set Value Of Selected Option That Populated Data From Database

I know maybe my question is very familiar, but i am a newbie in Codeigniter.我知道我的问题可能很熟悉,但我是 Codeigniter 的新手。 I have been already implement a solution through searching in Stackoverflow, but the problem it's still persists.我已经通过在Stackoverflow中搜索实现了一个解决方案,但问题仍然存在。 So i decided to ask again this question, through this forum.所以我决定通过这个论坛再次提出这个问题。

I am trying to insert a data into a PostgreSQL table, but the error tells me that i have a null value in my query.我正在尝试将数据插入 PostgreSQL 表,但错误告诉我我的查询中有一个空值 I see thats the value that comes from option is not has any value.我看到这就是来自选项的价值没有任何价值。

This is my code.这是我的代码。

Model模型

<?php
    class M_Item extends CI_Model{
        public function show_data(){
            return $this->db->get('tb_item');
        }

        public function add_item($data){
            $this->db->insert('tb_item', $data);
        }

        public function delete_item($where){
            $this->db->delete('tb_item', $where);
        }

        public function edit_item($where, $table){
            return $this->db->get_where($table,$where);
        }

        public function update_item($where, $data, $table){
            $this->db->where($where);
            $this->db->update($table, $data);
        }

        public function getAllItemName(){
            $query = $this->db->query('select item_name from tb_item;');
            return $query->result();
        }
    }
?>

Controller控制器

<?php
    class Purchase extends CI_Controller{

        public function __construct()
        {
            parent::__construct();
            $this->load->model(['m_purchase','m_inventory','m_item','m_grid','m_vendor']);
            $this->load->helper(['string','form']);
        }

        // Show index
        public function index(){
            $data['purchase'] = $this->m_purchase->show_data()->result();

            $this->load->view('templates/header');
            $this->load->view('templates/sidebar');
            $this->load->view('pages/transaksi/v_purchase', $data);
            $this->load->view('templates/footer');
        }

        // Show form for input vendor
        public function form_create(){
            $item = $this->m_item->getAllItemName();
            $grid = $this->m_grid->getAllGridName();
            $vendor = $this->m_vendor->getAllVendorName();

            $data = array(
                'item' => $item,
                'grid' => $grid,
                'vendor' => $vendor
            );

            $this->load->view('templates/header');
            $this->load->view('templates/sidebar');
            $this->load->view('pages/transaksi/v_purchase_create', $data);
            $this->load->view('templates/footer');
        }

//method add vendor
        public function add(){

            $purchase_item = new stdClass();
            date_default_timezone_set('Asia/Jakarta');
            $now = date('Y-m-d H:i:s');

            // User as dummy
            $userId = 1;


            $purchase_number = $this->input->post('purchase_id');
            $purchase_item = $this->input->post('item_id');
            $quantity = $this->input->post('quantity');
            $grid = $this->input->post('grid_id');
            $vendor = $this->input->post('vendor_id');

            $data = array(
                'purchase_id' => $purchase_number,
                'purchase_time' => $now,
                'item_id' => $purchase_item,
                'quantity' => (int)$quantity,
                'grid_id' => $grid,
                'vendor_id' => $vendor,
                'user_id' => $userId
            );

            $dataStock = array(
                'item_id' => $purchase_item,
                'grid_id' => $grid,
                'stock' => $quantity,
                'last_update' => $now
            );

            $this->db->trans_start();
            $this->m_purchase->add_purchase($data, 'tb_purchase');
            $this->m_inventory->updateStock($dataStock, 'tb_inventory');
            $this->db->trans_complete();
            if ($this->db->trans_status() === FALSE) {
                throw 
                $message = "Gagal Simpan data";
                echo "<script type='text/javascript'>alert('$message');</script>";
            } else {
                $success = "Sukses Simpan Data";
                echo "<script type='text/javascript'>alert('$success');</script>";
                redirect('pages/transaksi/purchase/index');
            }


        }

    //Another Code hiding

View看法

<div class="col-sm-6">
    <label for="">Purchase Item</label>
   <select name="item_id" id="item_id" class="form-control">
   <?php foreach ($item as $i) :{
    echo "<option value='".$i->item_id."'>".$i->item_name."</option>";
   }
   endforeach; ?>
   </select>
</div>

Error Result错误结果

ERROR: invalid input syntax for integer: "" LINE 1: ... VALUES ('PURCHASEOMS101', '2020-01-05 16:12:04', '', 100, '... ^

INSERT INTO "tb_purchase" ("purchase_id", "purchase_time", "item_id", "quantity", "grid_id", "vendor_id", "user_id") VALUES ('PURCHASEOMS101', '2020-01-05 16:12:04', '', 100, '', '', 1)

I was expected that the query has inserting some data to the table.我预计查询已将一些数据插入到表中。 Any help would be appreciated.任何帮助,将不胜感激。

Finally, after some searching and implementation, i found a solution for this case, i made a change on 2 file, the controller and the view最后,经过一些搜索和实现,我找到了解决这种情况的方法,我对 2 个文件、控制器和视图进行了更改

Purchase Controller采购控制器

// Show form for input purchase
        public function form_create(){  
            $data['item'] = $this->m_item->show_data()->result();
            $data['grid'] = $this->m_grid->show_data()->result();
            $data['vendor'] = $this->m_vendor->show_data()->result();

            $this->load->view('templates/header');
            $this->load->view('templates/sidebar');
            $this->load->view('pages/transaksi/v_purchase_create', $data);
            $this->load->view('templates/footer');
        }

        //method add vendor
        public function add(){
            date_default_timezone_set('Asia/Jakarta');
            $now = date('Y-m-d H:i:s');
            // User as dummy
            $userId = 1;

            $purchase_number = $this->input->post('purchase_id');
            $purchase_item = $this->input->post('purchase_item');
            $quantity = $this->input->post('quantity');
            $grid = $this->input->post('grid_id');
            $vendor = $this->input->post('vendor_id');

            $data = array(
                'purchase_id' => $purchase_number,
                'purchase_time' => $now,
                'item_id' => (int) $purchase_item,
                'quantity' => (int) $quantity,
                'grid_id' => (int) $grid,
                'vendor_id' => (int) $vendor,
                'user_id' => $userId
            );

            $dataStock = array(
                'item_id' => $purchase_item,
                'grid_id' => $grid,
                'stock' => $quantity,
                'last_update' => $now
            );

            $this->db->trans_start();
            $this->m_purchase->add_purchase($data, 'tb_purchase');
            $this->m_inventory->updateStock($dataStock, 'tb_inventory');
            $this->db->trans_complete();
            if ($this->db->trans_status() === FALSE) {
                throw 
                $message = "Gagal Simpan data";
                echo "<script type='text/javascript'>alert('$message');</script>";
            } else {
                $success = "Sukses Simpan Data";
                echo "<script type='text/javascript'>alert('$success');</script>";
                redirect('pages/transaksi/purchase/index');
            }


        }

//another code hiding

V_Purchase_Create V_Purchase_Create

<div class="col-sm-6">
    <label for="">Purchase Item</label>
      <select name="purchase_item" id="item_id" class="form-control">
        <?php foreach($item as $row):?>
          <option value="<?php echo $row->item_id;?>"><?php echo $row->item_name;?> 
          </option>
        <?php endforeach;?>
      </select>
</div>

So maybe this is not the best practice, (maybe you can do something like this with AJAX) but i think it's oke, thanks to @mikeyhun for answering my question, happy coding!!所以也许这不是最佳实践,(也许你可以用 AJAX 做这样的事情)但我认为没关系,感谢@mikeyhun 回答我的问题,快乐编码!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PHP中从数据库填充的下拉列表中设置选定的值 - Set selected value on dropdown populated from database in php 从 select 菜单中选择一个选项,将数据填充到数据库中的下一个 select 菜单。当找不到数据时如何显示“无可用数据” - as an option is selected from the select menu populated data to next select menu from database.How to show “No data availabe” when no data found 如何设置选定的填充 select 选项 - How to set selected populated select option 从数据库填充的下拉列表未使用PHP记录所选选项 - Dropdown populated from database doesn't record selected option with PHP 使用代码点火器和数据库中的数据选择的选项 - Option selected with codeigniter and data from DB 如果在ci中提交时选择,如何设置选择选项,从数据库中获取选项值? - how to set select option if selected on submit in ci ,options value fetching from the database? 如何获得选定的选项值(codeigniter) - How to get selected option value (codeigniter) 如何在codeigniter的选择框中显示从数据库中选择的值 - how to show the selected value from database in select box in codeigniter 从数据库中填充的下拉列表中获取选定的值 - Get selected value from drop down which is populated from a database 使用 Select 选项显示来自数据库的数据 Codeigniter - Display data from database with Select Option Codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM