【问题标题】:Codeigniter 3: Insert array form to databaseCodeigniter 3:将数组形式插入数据库
【发布时间】:2017-11-22 04:56:24
【问题描述】:

我是 Codeigniter 的新手,在将数组插入数据库时​​遇到了一些问题。不断弹出一个错误,我无法将数组插入到我的数据库中。

这是我的 HTML 表单:

<?= form_open('profile/profile_submit'); ?>
<table class="table table-hover table-striped table-responsive">
    <thead>
    <tr>
        <th id="headCheckHide"></th>
        <th><center>Name</center></th>
        <th><center>Date of Birth</center></th>
        <th><center>Occupation</center></th>
        <th><center>Educ. attainment</center></th>
    </tr>
    </thead>
    <tbody id="sibTable">
    <tr class="product-item">
            <td>
                <input type="text" class="form-control form-input" name="siblingName[]">
            </td>
            <td>
                <input type="text" class="form-control form-input" name="siblingBirthDate[]">
            </td>
            <td>
                <input type="text" class="form-control form-input" name="siblingOccupation[]">
            </td>
            <td>
                <select class="form-control form-input" name="siblingEducAttainment[]" >
                <option value="" selected>-Please select-</option>
                <option value="less than high school">less than high school</option>
                <option value="Some college but no degree">Some college but no degree</option>
                <option value="Associates Degree">Associates Degree</option>
                <option value="Elementary Graduate">Elementary Graduate</option>
                <option value="Secondary Graduate">Secondary Graduate</option>
                <option value="College Graduate">College Graduate</option>
                <option value="Master's Degree">Master's Degree</option>
                <option value="Professional Degree">Professional Degree</option>
                <option value="Doctorate Degree">Doctorate Degree</option>
                </select>
            </td>   
        </tr>
    </tbody>
</table>
<?= form_submit('submit','Save', 'class="btn btn-outline-primary waves-effect"');?>
<?php echo form_close();?>

我的表单控制器

 public function profile_submit(){
    $siblings=array(

        'name' => $this->input->post('siblingName'),
        'birthDate' => $this->input->post('siblingBirthDate'),
        'occupation' => $this->input->post('siblingOccupation'),
        'educAttainment' => $this->input->post('siblingEducAttainment')
    );

    $this->profile_model->submit_profile($siblings);
    redirect('profile','refresh'); //name of the html file
}

我的 (profile_model.php) 模型

 function submit_profile($siblings){
        $this->db->insert('tbl_st_profile_sibling',$siblings);
}

这是我的模型错误:无法将数组插入数据库。 有人可以帮忙吗?谢谢。

【问题讨论】:

  • 如果你也给我们错误信息会更好。

标签: php mysql arrays codeigniter


【解决方案1】:

我认为您需要一次插入多个具有相同名称的输入数据,别担心,请按照我的说明进行操作,如下所示

控制器的变化:

public function profile_submit(){
    $submit_status = $this->profile_model->submit_profile();
    if( $submit_status == "TRUE"){
     redirect('profile','refresh'); //name of the html file
    }else{
     // Do Something else..
    }

}

模型变化:

function submit_profile(){

      $siblingsCount = count($this->input->post('siblingName')); 
      if($siblingsCount != null){
         $itemValues=0;
         $query = "INSERT INTO tbl_st_profile_sibling(name,birthDate,occupation,educAttainment) VALUES ";
         $queryValue = "";
         for($i=0;$i<$siblingsCount;$i++) {
             $name = $this->input->post('name');
             $birthDate = $this->input->post('birthDate');
             $occupation = $this->input->post('occupation');
             $educAttainment = $this->input->post('educAttainment');
             if(!empty($name[$i])) {
                 $itemValues++;
                 if($queryValue!="") {
                     $queryValue .= ",";
                 }

                 $queryValue .= "('" . $name[$i] . "', '" . $birthDate[$i] . "', '" . $occupation[$i] . "', '" . $educAttainment[$i] . "')";
             }
         }
       $sql = $query.$queryValue;
       if($itemValues!=0) {
        if (!$this->db->query($sql)) {
         echo "FALSE";
        }else {
         echo "TRUE";
        }
       }
      }
}

我希望这可以帮助你...谢谢!

【讨论】:

    【解决方案2】:

    如果您需要将数组数据存储到数据库中,您需要使用循环插入一个。

    function submit_profile($siblings){
       foreach($siblings['name'] as $key => $siblingName) {
          $dataToSave = array(
             'name' => $siblingName,
            'birthDate' => $siblings['siblingBirthDate'][$key],
            'occupation' => $siblings['siblingOccupation'][$key],
            'educAttainment' => $siblings['siblingEducAttainment'][$key]
          );
          $this->db->insert('tbl_st_profile_sibling', $dataToSave);
       }
    }
    

    更新:即使您可以使用 insert_batch 跳过循环插入

    function submit_profile($siblings){
       foreach($siblings['name'] as $key => $siblingName) {
          $dataToSave[] = array(
            'name' => $siblingName,
            'birthDate' => $siblings['siblingBirthDate'][$key],
            'occupation' => $siblings['siblingOccupation'][$key],
            'educAttainment' => $siblings['siblingEducAttainment'][$key]
          );
       }
          $this->db->insert_batch('tbl_st_profile_sibling', $dataToSave); 
    
    }
    

    【讨论】:

      【解决方案3】:

      问题不在于您保存到数据库中的数组,实际问题在于您采用数组格式兄弟名称 [] 的帖子字段。所以你需要把then转换成字符串,这样就可以方便的保存了。

      将数组转换为逗号分隔的列表,然后保存到数据库中。

          $siblingName = implode(",",$_POST['siblingName']);
          $siblingBirthDate= implode(",",$_POST['siblingBirthDate']);
          $siblingOccupation= implode(",",$_POST['siblingOccupation']);
          $siblingEducAttainment= implode(",",$_POST['siblingEducAttainment']);
      
      
         $siblings=array(
      
            'name' => $siblingName,
            'birthDate' => $siblingBirthDate,
            'occupation' => $siblingOccupation,
            'educAttainment' => $siblingEducAttainment
         );
      

      【讨论】:

      • 我每行插入数据。但是,感谢您的帮助:)。这可能会对我的一些代码在开发过程中有所帮助
      【解决方案4】:
      <input type="hidden" name="count" value="<?php echo count($var); ?>" />
      
      
      for($i=0;$i < count($var);$i++)
      {
      
          <tr class="product-item">
              <td>
                  <input type="text" class="form-control form-input" name="siblingName<?php echo $i; ?>">
              </td>
              <td>
                  <input type="text" class="form-control form-input" name="siblingBirthDate<?php echo $i; ?>">
              </td>
              <td>
                  <input type="text" class="form-control form-input" name="siblingOccupation<?php echo $i; ?>">
              </td>
              <td>
                  <select class="form-control form-input" name="siblingEducAttainment<?php echo $i; ?>" >
                  <option value="" selected>-Please select-</option>
                  <option value="less than high school">less than high school</option>
                  <option value="Some college but no degree">Some college but no degree</option>
                  <option value="Associates Degree">Associates Degree</option>
                  <option value="Elementary Graduate">Elementary Graduate</option>
                  <option value="Secondary Graduate">Secondary Graduate</option>
                  <option value="College Graduate">College Graduate</option>
                  <option value="Master's Degree">Master's Degree</option>
                  <option value="Professional Degree">Professional Degree</option>
                  <option value="Doctorate Degree">Doctorate Degree</option>
                  </select>
              </td>   
          </tr>
      
      }
      
      
      $count = $this->input->post('count');
      for($i=0;$i < $count;$i++)
      {
          $siblings=array(
              'name' => $this->input->post('siblingName'.$i),
              'birthDate' => $this->input->post('siblingBirthDate'.$i),
              'occupation' => $this->input->post('siblingOccupation'.$i),
              'educAttainment' => $this->input->post('siblingEducAttainment'.$i)
          );
      
          $this->profile_model->submit_profile($siblings);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-09
        • 1970-01-01
        • 2014-05-04
        • 1970-01-01
        • 2021-09-19
        相关资源
        最近更新 更多