php - 数据未插入数据库
问题描述
我已经从表单中输入了数据,但条目为 0。
我尝试使用数组输入数据,但在数据库中输入了值 0
看法
<div class="form-row">
<div class="col-md->
<?php foreach($komoditi->result_array() as $k): ?>
<p><?= $k['komoditi'] ?></>
<input type="hidden" name="id_bahan[]" value="<?= $k['id_bahan']; ?>">
<?php endforeach; ?>
</div>
<div class="col-md-2">
<?php foreach($komoditi->result_array() as $k): ?>
<input type="number" name="hargabahanpokok[]">
<?php endforeach; ?>
<div class="col-md-2">
<?php foreach($komoditi->result_array() as $k): ?>
<p>/<?= $k['satuan']; ?></p>
<?php endforeach; ?>
</div>
<input type="hidden" name="id_pasar[]" value="3">
<input type="hidden" name="status[]" value="1">
<input type="hidden" name="id_hargabahanpokok" value="<?php echo $satu['id_hargabahanpokok'];?>">
<input type="submit" name="simpan"value="Simpan">
模型
public function insert($data){
return $this->db->insert('hargabahanpokok',$data);
}
控制器
public function index(){
if($this->input->post('simpan')){
$id_pasar = $this->input->post('id_pasar');
$id_bahan = $this->input->post('id_bahan');
$hargabahanpokok = $this->input->post('hargabahanpokok');
$status = $this->input->post('status');
$query = $this->db->query("SELECT count('id_bahan') as jml from namabahan")->result_array();
$max = intval($query[0]['jml']);
$id_pasar = 3;
$id_bahan = $this->input->post('id_bahan');
$hargabahanpokok = $this->input->post('hargabahanpokok');
$status = 1;
for($i=0; $i<$max; $i++){
$result = array(
`id_pasar` => 3,
`id_bahan` => $id_bahan[$i],
`hargabahanpokok` => $hargabahanpokok[$i],
`status` => 1,
);
$this->ModelTaraju->insert('hargabahanpokok', $result);
}
}
}
我预计:
进入数据库的数据是在 'hargabahanpokok' 1000 的数据库中输入 'hargabahanpokok' 的 1000 时。
id_bahan 根据表 bahan 中的 id_bahan 进入数据库
ID_pasar根据表pasar中的ID_pasar进入数据库
解决方案
在插入之前打印数据数组中的值并查看其中的值,例如
public function insert($data){
print_r($data);exit;
$this->db->insert('hargabahanpokok',$data);
}
推荐阅读
- android - 使用 startActivity(Intent) 和 handler.postDelayed() 显示屏幕
- javascript - 如何使用量角器从下拉列表中选择一个值?
- docker - “/bin/sh”:未找到命令,但仅在第二个构建步骤中
- apache-spark - 访问 Hive 中已经存在的表
- javascript - 嵌套查询在 Node.js 和 MySQL 中不起作用
- c# - 如何在 C# 中向数据库中插入数据
- android - 在传递单击的自定义列表项的数据时单击带有 RecyclerView 的片段的项目时如何打开新活动?
- php - 防止列低于零和回显警告
- c - helloworld 内核模块中的 insmod 错误:模块格式无效
- python-3.x - 分组依据和 values_counts