2017-11-22 2 views
0

Ich bin neu mit Codeigniter und habe Probleme beim Einfügen von Arrays in meine Datenbank. Es ist ein Fehler aufgetreten, dass ich das Array nicht in meine Datenbank einfügen kann.Codeigniter 3: Array-Formular in Datenbank einfügen

Hier ist mein HTML-Formular:

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

Mein Controller für 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 
} 

Mein Modell für (profile_model.php)

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

Dies ist mein Modell mit dem Fehler: kann Array nicht in die Datenbank einfügen. Kann mir bitte jemand helfen? Danke.

+0

Es wird besser sein, wenn Sie uns die Fehlermeldung auch geben. –

Antwort

1

Wenn Sie Array-Daten in der DB speichern müssen, müssen Sie Schleifen verwenden, um eine einzufügen.

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: Auch können Sie insert_batch verwenden Einfügung in Schleife überspringen

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

} 
+0

danke für die Hilfe :) –

2

Das Problem ist nicht mit dem Array, das Sie in der Datenbank speichern, das eigentliche Problem ist mit den Post-Felder Thy sind im Array-Format GeschwisterName []. Sie müssen also in Strings konvertieren, damit dies leicht gespeichert werden kann.

konvertieren Sie das Array in Koma getrennte Liste und dann in der Datenbank speichern.

+0

Ich füge die Daten in separaten Zeilen ein. Aber danke für die Hilfe :). Dies könnte mir in meinem Code durch die Entwicklung helfen –

2

Ich glaube, Sie mehrere Eingangsdaten zu einer Zeit mit dem gleichen Namen einfügen müssen, keine Sorge, folgen Sie bitte meine Anweisungen wie unten

Änderungen im Steuer gegeben:

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

} 

Änderungen im Modell:

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

Ich hoffe, dass dies Ihnen helfen kann ... Danke!

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