我知道也许我的问题很熟悉,但我是Codeigniter的新手。我已经通过在 Stackoverflow 中搜索实现了解决方案,但它仍然存在问题。所以我决定通过这个论坛再问这个问题。
我正在尝试将数据插入 PostgreSQL 表中,但错误告诉我我的查询中有一个空值。我看到来自选项的价值没有任何价值。
这是我的代码。
型
<?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();
}
}
?>
控制器
<?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
视图
<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: 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)
我预计查询已向表中插入了一些数据。任何帮助将不胜感激。
最后,经过一些搜索和实现,我找到了这种情况的解决方案,我对 2 个文件、控制器和视图进行了更改
采购控制器
// 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
<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>
所以也许这不是最佳实践,(也许你可以用 AJAX 做这样的事情(,但我认为没关系,感谢@mikeyhun回答我的问题,快乐编码!!