0

I'm new with Codeigniter and I'm having some trouble inserting arrays into my database. An error kept popping out that I can't insert array to my database.

Here's my HTML form:

<?= 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();?>

My Controller for the form

 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
}

My model for (profile_model.php)

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

this is my model with the error: can't insert array into database. Can anyone please help? thank you.

1
  • It will be better if you give us the error message as well. Commented Nov 22, 2017 at 5:00

4 Answers 4

2

I think you need to insert multiple input data at a time with the same name, don't worry please follow my instructions as given below

Changes in Controller:

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..
    }

}

Changes in Model:

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";
        }
       }
      }
}

I hope this may help you...Thanks!

Sign up to request clarification or add additional context in comments.

Comments

1

If you need to store array data into DB you need to use loops to insert one.

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);
   }
}

UPDATE : Even you can use insert_batch to skip insertion in loop

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); 

}

Comments

1

The issue is not with the array you are saving into database the actual issue is with the post fields thy are in array format siblingName[]. So you need to convert then into strings so that this can be saved easily.

convert the array into coma separated list and then save in database.

    $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
   );

1 Comment

I'm inserting the data per separate rows. However, thanks for the help :). This might help me in some of my code through the development
1
<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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.