首页 > 解决方案 > 在 CodeIgniter 中保存不完整的数据(视图到控制器)

问题描述

保存不完整的数据(部分 Enter Some value 为 NULL),当我保存时,我有 5 个带有 200 个数据的变量文本框(要保存)。不想保存所有数据(部分保存)。只有 150。但是当我在视图中将文本框减少到 4 个时(例如,我删除了文本框 onhand2)。存储更多数据。多达 170 个。当我将文本框减少到 1(我只留下 iproduct)时,所有数据都被存储了。没有错误,但看起来视图发送的数据变量没有被控制器捕获。例如,在第 151 行,数据库中存储的数据为空。问题的结论:我在views中添加的文本框越多,虽然在controllers中循环了200个,但是最多200个controllers中的变量值都不是null,但存储的数据却越少)

CREATE TABLE tm_schedulebulanan_item
(
  i_product character(11) NOT NULL,
  e_product_name character varying(200) NOT NULL,
  n_target numeric(5,0),
  onhand numeric(5,0),
  onhand2 numeric(5,0),
  CONSTRAINT pk_tm_schedulebulanan_item PRIMARY KEY (i_product)
)

<? echo $this->pquery->form_remote_tag(array('url'=>'schedule-bulanan/cform','update'=>'#pesan','type'=>'post'));?>
<table class="maintable">
  <tr>
    <td align="left">
  <div id="masterstockopnameform">
  <div class="effect">
    <div class="accordion2">
      <input type="text" name="jml" id="jml" value="230">
      <table class="table">
        <tr class="success">
    <td width="19%">Periode</td>
    <?  $query3 = $this->db->query(" select i_periode from tm_periode ");
          if ($query3->num_rows() > 0){
              $hasilrow = $query3->row();
              $i_periode  = $hasilrow->i_periode;
                }
    ?>

    <td width="1%">:</td>
    <td width="80%"><input type="text" readonly id="dschedule" name="dschedule" value="<? if($dschedule) echo $dschedule; ?>"
      onclick="showCalendar('',this,this,'','dschedule',0,20,1)">
        <input type="hidden" id="ischedule" name="ischedule" value="<? if($ischedule) echo $ischedule; ?>"></td>
        <input type="hidden" id="periode" name="periode" value="<? if($i_periode) echo $i_periode; ?>">
        </tr>
                <tr class="success">
    <td width="19%">Jenis Schedule</td>
    <td width="1%">:</td>
    <td width="80%"><input   type="text" readonly id="ejenisshedule" name="ejenisshedule" value="<? if($ejenisshedule) echo $ejenisshedule; ?>"
      onclick='showModal("schedule-bulanan/cform/jenis/","#light");jsDlgShow("#konten *", "#fade", "#light");'>
        <input type="hidden" id="ijenisschedule" name="ijenisschedule" value="<? if($ijenisschedule) echo $ijenisschedule; ?>"></td>
        </tr>

        <tr class="success">
    <td width="19%">Category</td>
    <td width="1%">:</td>
    <td width="80%"><input   type="text" readonly id="eproductcategoryname" name="eproductcategoryname" value="<? if($eproductcategoryname) echo $eproductcategoryname; ?>"
      onclick='showModal("schedule-bulanan/cform/category/"+document.getElementById("ijenisschedule").value+"/"+document.getElementById("dschedule").value+"/","#light");jsDlgShow("#konten *", "#fade", "#light");'>
        <input type="hidden" id="iproductcategory" name="iproductcategory" value="<? if($iproductcategory) echo $iproductcategory; ?>"></td>
        </tr>

      <tr class="success">
      <td>Estimasi Sisa</td>
      <td>:</td>
      <td><input  type="text" id="estimasiheader" name="estimasiheader" onkeypress="return isNumberKey(this.value)" value="0" onkeyup="hitung();" ></td>
      </tr> 

      <tr class="success">
      <td>Kebutuhan Minggu 1</td>
      <td>:</td>
      <td><input  type="text" id="keb1minggu" name="keb1minggu" value="0" onkeypress="return isNumberKey(this.value)" onkeyup="hitung();" ></td>
      </tr> 

      <tr class="success">
      <td>Kebutuhan Minggu 2</td>
      <td>:</td>
      <td><input  type="text" id="keb2minggu" name="keb2minggu" value="0" onkeypress="return isNumberKey(this.value)" onkeyup="hitung();" ></td>
      </tr> 

            <tr class="success">
      <td>Kebutuhan Minggu 3</td>
      <td>:</td>
      <td><input  type="text" id="keb3minggu" name="keb3minggu" value="0" onkeypress="return isNumberKey(this.value)" onkeyup="hitung();" ></td>
      </tr> 

            <tr class="success">
      <td>Kebutuhan Minggu 4</td>
      <td>:</td>
      <td><input  type="text" id="keb44minggu" name="keb44minggu" value="0" onkeypress="return isNumberKey(this.value)" onkeyup="hitung();" ></td>
      </tr> 

        <tr class="success">
    <td width="19%">&nbsp;</td>
    <td width="1%">&nbsp;</td>
    <td width="80%">
      <input name="login"  class="btn btn-info btn-xs" id="login" value="Simpan" type="submit" onclick="dipales(parseFloat(document.getElementById('jml').value));">
      <input name="cmdreset" id="cmdreset" value="Keluar" class="btn btn-danger btn-xs" type="button" onclick='show("schedule-bulanan/cform/","#main")'>
      
    </td>
        </tr>
      </table>

      <? if($detail){?>
  <div id="detailheader" align="center">
   <table id="myTable" class="table" style="width:750px;">
 <th width="30px" align="center">No</th>
          <th width="25px" align="center">Kd Barang</th>
          <th width="150px" align="center">Nama Barang</th>
          <th width="87px" align="center">Target</th>
          <th width="87px" align="center">On Hand</th>
          <th width="87px" align="center">onhand2</th>
      <? } ?>
      </div>
      <div id="detailisi" align="center">
<? if($detail){
  $i++;    
 echo"<tr  class='warning'>
   <td style='width: 27px' class='warning' align=right>$i</td>
   <td><input  style='width: 100px' readonly type='text' class='warning' id='iproduct".$i."' name='iproduct".$i."' value='$isi->i_product'></td>
   <td><input type='text' style='width: 300px' class='warning' id='eproductname".$i."' name='eproductname".$i."' value='$isi->e_product_name'></td>
   <td><input type='text'  style='width: 50px' class='warning' readonly id='target".$i."' name='target".$i."'  value='$isi->target'></td>
   <td><input type='text'  style='width: 50px' class='warning' readonly id='onhand".$i."'  name='onhand".$i."'  value='$onhand'></td>
   <td><input type='text'  style='width: 50px' class='warning' readonly id='onhand2".$i."'  name='onhand2".$i."'  value='$onhand2'></td>
        </tr>";
         } ?>
        </table>        
      <? } ?>
      </div>
<div id="pesan"></div>

    </div>
  </div>
  </div>
  <?=form_close()?>
    </td>
  </tr>
</table>

控制器

controllers
jmlx=200;
for($i=1;$i<=$jmlx;$i++){
$iproduct            = $this->input->post('iproduct'.$i, TRUE);
$eproductname                = $this->input->post('eproductname'.$i, TRUE);
$target                = $this->input->post('target'.$i, TRUE);
$onhand                = $this->input->post('onhand'.$i, TRUE);
$onhand2            = $this->input->post('onhand2'.$i, TRUE);

//Insert Data Ke Models
$this->mmaster->insertdetail($iproduct,$eproductname,$target,$onhand,$onhand2,$i); 
//End
   
if (($this->db->trans_status() === FALSE))
        {
           $this->db->trans_rollback();
          }else{
           $this->db->trans_commit();
        }
      }

楷模

function insertdetail($iproduct,$eproductname,$target,$onhand,$onhand2,$i)
    {
      $this->db->set(
        array(
        'i_product'          => $iproduct,
        'e_product_name'     => $eproductname,
        'target'             => $target,
        'onhand'             => $onhand,
        'onhand2'            => $onhand2,
        'n_item_no'          => $i
        )
      );
      
      $this->db->insert('tm_schedulebulanan_item');
    }

标签: htmlloopscodeigniterfor-loop

解决方案


推荐阅读