如何从视图中获取数据数组?以及如何插入我的数据库?
我的观点,我尝试了很多代码,但仍然是一个错误,很抱歉我的问题和我糟糕的英语
<form role="form" action="<?php echo base_url().'potongan/validasi'?>" action="GET">
<div class="box-body">
<div class="box-header with-border">
<div class="row">
<div class="col-xs-3">
<label >NIM</label>
<input type="text" class="form-control" name="nim" id="nim" placeholder="NIM" value="<?php echo $nime ?>" readonly>
</div>
<div class="col-xs-3">
<label >Nama Mahasiswa</label>
<input type="text" class="form-control" name="namamhs" placeholder="Nama Mahasiswa" value="<?php echo $nim[0]->namamhs ?>" disabled>
</div>
<div class="col-xs-3">
<label >Jurusan</label>
<input type="text" class="form-control" name="jurusan" placeholder="Jurusan" value="<?php echo $nim[0]->jurusan ?>" disabled>
</div>
</div>
<br>
<div class="form-group">
<label >Kode Beasiswa</label>
<input type="text" class="form-control " name="kdbeasiswa" value="<?php echo $kdbeasiswa ?>" readonly>
</div>
<div class="form-group">
<label >Keterangan</label>
<input type="text" class="form-control " name="keterangan" value="<?php echo $keterangan ?>" readonly>
</div>
<div>
<table class="table table-bordered table-striped">
<tr>
<th></th>
<th>Kode</th>
<th>Deskripsi</th>
<th>Tahun Akademik</th>
<th>semester</th>
<th>Tagihan</th>
<th>Beasiswa</th>
<th>Terbayar</th>
<th>Potongan</th>
</tr>
<?php foreach ($nim as $n): ?>
<tr>
<td>
<input type="hidden" class="form-control " name="id[]" value="<?php echo $n->thakad.$n->smtakad.$n->kdkeu; ?>" maxlength="3" >
</td>
<td><input type="hidden" class="form-control " name="kdkeu[]" value="<?php echo $n->kdkeu; ?>" maxlength="3" ><?php echo $n->kdkeu; ?></td>
<td><?php echo $n->deskeu; ?></td>
<td><input type="hidden" class="form-control " name="thakad[]" value="<?php echo $n->thakad; ?>" maxlength="3" ><?php echo $n->thakad; ?></td>
<td><input type="hidden" class="form-control " name="smtakad[]" value="<?php echo $n->smtakad; ?>" maxlength="3" ><?php echo $n->smtakad_view; ?></td>
<td><?php echo $n->tagihan; ?></td>
<td><?php echo $n->beasiswa; ?></td>
<td><?php echo $n->terbayar; ?></td>
<td>
<input type="text" class="form-control " name="prosentase_bea[]" value="<?php echo $n->potongan; ?>" maxlength="3">
</td>
</tr>
<?php endforeach ?>
</table>
</div>
</div>
<br>
<button class="btn btn-info" style="float: right;">submit</button>
</div>
<!-- /.box-body -->
</form>
当我没有向数据库提交数据插入并且出现类似的错误时,我的控制器看起来是这样的
文件名:C:\examplep/htdocs/tagihan/system/database/DB_query_builder.php行号:1498
$data = array();
$count = count($this->input->post('device'));
for($i=0; $i<$count; $i++) {
$data[] = array(
$thakad= $this->input->get('thakad'),
$smtakad= $this->input->get('smtakad'),
$kb = $this->input->get('kdbeasiswa'),
$kk = $this->input->get('kdkeu'),
$nim= $this->input->get('nim'),
$pb = $this->input->get('prosentase_bea'),
$status = 1,
$user = $this->session->userdata('username'),
$kt = $this->input->get('keterangan'),
$curentDate = date("Y-m-d"));
}
$this->db->insert_batch('keu_beasiswapermhs',$data);
redirect('http://localhost/tagihan/potongan');
养成使用正确MVC 的习惯
查看
<form action="URL" method="post">
</form>
控制器
$this->model->function_name();
redirect('URL');
模型
function function_name(){
$dataArr = array(
'field_name'=>$this->input->post('form_input_name'),
);
$this->db->INSERT('tbl_name',$dataArr);
}
您有诸如"namamhs"one_answers"jurusan"之类的字段没有放入数据库中。
您不需要for或while循环来插入数据
您没有分配数据库中每个发布项目的字段。您的控制器不知道每个值应该插入到哪里。不要用get,你应该用post。这里有一个控制器的例子,你可以适应你的形式:
$data = [
'url' => $this->input->post('url'),
'name' => $this->input->post('name'),
'genre' => $this->input->post('genre'),
'counter' => $this->input->post('counter'),
'type' => $this->input->post('type')
];
其中"url"是数据库中的一个字段,您将$this->input->post('url'(放入其中。接下来您检查值:
$this->form_validation->set_rules('url', 'url', 'required|trim');
$this->form_validation->set_rules('name', 'Name', 'required|trim|callback_trims');
$this->form_validation->set_rules('genre', 'Genre', 'required|trim');
$this->form_validation->set_rules('counter', 'Counter', 'numeric');
$this->form_validation->set_rules('type', 'Type', 'required|min_length[3]');
如果值通过验证,则插入是简单的
if( $this->form_validation->run() == FALSE) {
echo validation_errors();
}else
{
$this->db->insert('music', $data);
}
您不需要批量插入。只需使用插入
您的$data数组为空。。。在插入批处理之前,始终检查数组是否为空。。。导致错误DB_query_builder.php行号:1498