我使用以下代码
我的控制器
public function index() {
$pesan['title'] = 'Poster';
/* form validation size & paper type */
$this->form_validation->set_rules('quantityposter', 'Quantity', 'required');
$this->form_validation->set_rules('priceposter', 'Price', 'required');
if ($this->form_validation->run() == FALSE) {
/* view */
} else {
$data['size'] = $this->input->post('sizeposter');
$data['papertype'] = $this->input->post['papertypeposter'];
$data['quantity'] = $this->input->post['quantityposter'];
$data['harga'] = $this->input->post['priceposter'];
$this->poster_model->daftar($data);
redirect(site_url('home'));
}
}
我的模特
public function daftar($data) { $this->db->insert('poster', $data); }
我的表格
<form action="<?php base_url('poster') ?>" method="post" class="form-horizontal">
/* size poster input */
/* paper type poster input */
<div class="form-group">
<label for="quantityposter" class="col-sm-2">Quantity</label>
<div class="col-sm-4">
<input type="text" class="form-control" onchange="fetch_quantity(this.value);" name="quantityposter" value="<?php echo set_value('quantityposter', '1'); ?>">
</div>
<p><?php echo form_error('quantityposter'); ?></p>
</div>
<div class="form-group">
<label for="priceposter" class="col-sm-2">Price</label>
<div class="col-sm-4" id="price">
<input type="text" name="priceposter" class="form-control" value="<?php echo "50000"; ?>">
</div>
<p><?php echo form_error('priceposter'); ?></p>
</div>
<br>
/* submit input */
</form>
我插入的数据成功,但是值为null?为什么呢?值有误吗?尺寸海报形式有效,但纸型,数量和harga为空?为什么?
答案 0 :(得分:1)
问题似乎出在您的控制器上。
在您使用else语句之后
$data['papertype'] = $this->input->post['papertypeposter'];
$data['quantity'] = $this->input->post['quantityposter'];
$data['harga'] = $this->input->post['priceposter'];
我认为一定是
$data['papertype'] = $this->input->post('papertypeposter');
$data['quantity'] = $this->input->post('quantityposter');
$data['harga'] = $this->input->post('priceposter');
只需使用括号而不是括号。希望这会有所帮助!