Welcome Guest, Not a member yet? Register   Sign In
how to input multiple values and data at the same time ?
#1

So i have a problem with many to many relationship. Currently i have "surat" and "surat_user" table.
How do i insert data into surat and at the same time insert multiple values from select2 multiple form into surat_user table ?
and how to get data so i can update it.

I've seen some of other similar question but none of them suitable for my problem. I've tried using foreach to store the multiple values but it doesn't work 
With these codes below i get some error when i tried to input data
"Object of class CI_DB_mysqli_result could not be converted to string" 
"Array to string conversion" 
"Cannot add or update a child row: a foreign key constraint fails"

Model:

Code:
var $table='surat';

public function add_sm($data) {
   $this->db->insert($this->table,$data);
   return $this->db->insert_id();
}

View:

Code:
<label class="control-label col-lg-2">Disposisi</label>
         <div class="col-lg-5">
           <select class="form-control select2 id_user" name="id_user[]" style="width: 100%;" multiple="multiple">
               <option value=""></option>
               <?php
               $query = $this->db->query("SELECT * FROM user");
              foreach ($query->result() as $row) { ?>
               <option value="<?php echo $row->id_user; ?>"> <?php echo $row->nama; ?></option>

               <?php  
               }
               ?>
               </select>
         </div>
       </div>

Controller :

Code:
class SuratMasuk extends CI_Controller {
public function add_sm_proses(){
 $data = array(
           'no_surat'=>$this->input->post('no_surat'),
           'id_status'=>$this->input->post('id_status'),  
      );
 $id_surat =  $this->db->query('SELECT surat.id_surat FROM surat ORDER BY id_surat DESC limit 1');
 $id_user = $this->input->post(array('id_user'));
 foreach ($id_user as $u){
   $this->db->query("INSERT INTO surat_user (id_surat,id_user) VALUES ('$id_surat','$u')");
}
$insert = $this->M_sm->add_sm($data);
if($insert=1){
       redirect('SuratMasuk');
   }   else {
       echo "<h2>Gagal menambahkan data</h2>";

   }
}
Reply
#2

PHP Code:
$id_surat $this->db->query('SELECT surat.id_surat FROM surat ORDER BY id_surat DESC limit 1');
$id_user $this->input->post(array('id_user')); 

I think you expect both these to return single value, I'm pretty sure both, or at least first, is returning either array or DB query result object.

For first line, you probably want to do something like this:
PHP Code:
$id_surat $this->db->query('SELECT surat.id_surat FROM surat ORDER BY id_surat DESC limit 1')->first_row()->id_surat 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB