首页 > 解决方案 > 数据未插入数据库

问题描述

我已经从表单中输入了数据,但条目为 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);
            }
        }
}

我预计:

  1. 进入数据库的数据是在 'hargabahanpokok' 1000 的数据库中输入 'hargabahanpokok' 的 1000 时。

  2. id_bahan 根据表 bahan 中的 id_bahan 进入数据库

  3. ID_pasar根据表pasar中的ID_pasar进入数据库

标签: phpmysqlcodeigniter

解决方案


在插入之前打印数据数组中的值并查看其中的值,例如

public function insert($data){
   print_r($data);exit;
   $this->db->insert('hargabahanpokok',$data);   
}

推荐阅读