Welcome Guest, Not a member yet? Register   Sign In
I can't update multiple rows.
#1

[eluser]Faisal Alghamdi[/eluser]
Hi everybody,

I am developing a control panel for my website.

And, I have been trying to update multiple records in my db.

But, the point is that I could not make a successful attempt to update more than one.

I have some records that should be either published or rejected.. I used drop-down
I was able to have forms in each row but in this case I have to update every record individually which is time-consuming.

Help me in debugging it and figure out what caused the problem, please

controller
Code:
function records_approval() {
  
  $this->admin_is_logged_in();
  $data = array (

  'mod_name' => $this->admin_model->mod_name(),
  'JOB_NAME' => array('name'=>'JOB_NAME', 'size'=>30),
  'CITY' => array('name'=>'CITY', 'size'=>30),
  'approval' => array('name'=>'approval', 'size'=>30),
  'idRECORD' => array('name'=>'idRECORD', 'size'=>30),
  // utk
  'numbers' => $this->admin_model->records_approval(),
  'title' => 'records approvals',
  'records_approval' => 'admin/records_approval_view',
  );
  
  
  $this->load->view('cp_view', $data);
  
}

model
Code:
function upd() {
  
   $data = array(
    'JOB_NAME'=>array($this->input->post('JOB_NAME')),
    'CITY'=>array($this->input->post('CITY')),
    'approval'=>array($this->input->post('approval')),
    'idRECORD'=>array($this->input->post('idRECORD')),);

    
    $this->db->where('idRECORD',array($this->input->post('idRECORD')));
    if($this->db->update('main_records',$data)) {
    echo "Done";
    echo anchor("cp/records_approval","   Back  ");
}
    
}

view
Code:
<table  id="mytable" cellspacing="0"   border="0">
&lt;?php
    foreach($numbers as $row ){
    echo form_open('cp/edit','name="login"');
  
    echo "<td >$row->EMAIL</td>";
    
    
     echo "<td>".form_input($JOB_NAME,$row->JOB_NAME)."</td>";
     $total = $row->SALARY+=$row->BOUNS;
     echo "<td >".$total." SR"."</td>";
     echo "<td>". $row->CATE_NAME ."</td>";
     echo "<td>".form_input($CITY,$row->CITY)."</td>";
    
    

     echo "<td>";
    
     echo "<select id='approval' name='".$approval."'>";
    
     echo "<option value='0'> suspend </option>";
     echo "<option value='1'>publish</option>";
     echo "<option value='2'>reject</option>";

     echo "</select>".br();

     echo form_hidden($idRECORD,$row->idRECORD);
  
    
     echo "</td>";
    
      echo "</tr>";
    }
    
      
     echo form_submit('change','save changes');
     echo form_close();  

    ?&gt;
  </table>

the form sends to controller:: edit
Code:
function edit() {
  $this->admin_is_logged_in();
  

  if($this->input->post('change')){
   $this->admin_model->upd();
  }
  
  
}
#2

[eluser]Faisal Alghamdi[/eluser]
I changed the model a little bit.. I think there is the problem .. it's not working though

I used foreach because I read that It's not possible to update more than one row.
Code:
function upd() {
  
   $data = array(
    'JOB_NAME'=>$this->input->post('JOB_NAME'),
    'CITY'=>$this->input->post('CITY'),
    'approval'=>$this->input->post('approval'),
    'idRECORD'=>$this->input->post('idRECORD'),);

    foreach($data as $row) {
     echo $row->idRECORD;
    $this->db->where('idRECORD',$row->idRECORD);
    $this->db->update('main_records',$data);
    }
     echo "Done";
     echo anchor("cp/records_approval","   Back  ");  

}

#3

[eluser]Iszuddin Ismail[/eluser]
Try this
Code:
function upd() {
   $data = array();

   $jobs = $this->input->post('JOB_NAME');
   $cities = $this->input->post('CITY');
   $approvals = $this->input->post('approval');
   $records = $this->input->post('idRECORD');

   foreach( $records as $rec_key => $rec_val) {
      $data[] = array(
         'idRECORD' => $records[$rec_key],
         'JOB_NAME' => $jobs[$rec_key],
         'approval' => $approvals[$rec_key],
         'CITY' => $cities[$rec_key],
      );
   }

   $this->db->update_batch('main_records', $data, 'idRECORD');

   echo "Done";
   echo anchor("cp/records_approval","   Back  ");  
}
#4

[eluser]Faisal Alghamdi[/eluser]
thank you so much; it returns "less errors" :

Message: Invalid argument supplied for foreach()


#5

[eluser]Iszuddin Ismail[/eluser]
I assumed that your $records = $this->input->post('idRECORD') would return an array. If not, then we need to look at how you are building your form.
#6

[eluser]Faisal Alghamdi[/eluser]
I really reached the end of my rope, I tried so many solutions on the web and still not working

here is my view...

Code:
<table  id="mytable" cellspacing="0"   border="0">
    &lt;?php
    
  
  
    
    $a = 0;
    
    
         foreach($numbers as $row ){
         echo form_open('cp/records_approval','name="login"');

         echo "<td >$row->EMAIL</td>";


          echo "<td>".form_input($JOB_NAME,$row->JOB_NAME)."</td>";
          $total = $row->SALARY+=$row->BOUNS;
          echo "<td >".$total." SR"."</td>";
          echo "<td>". $row->CATE_NAME ."</td>";
          echo "<td>".form_input($CITY,$row->CITY)."</td>";



          echo "<td>";

          echo "<select id='approval' name='".$approval."'>";

          echo "<option value='0'> suspend </option>";
          echo "<option value='1'>publish</option>";
          echo "<option value='2'>reject</option>";

          echo "</select>".br();

          echo form_hidden('idRECORD',$row->idRECORD);


          echo "</td>";

           echo "</tr>";
         }


          echo form_submit('change','save changes');
          echo form_close();  

         ?&gt;
       </table>




Theme © iAndrew 2016 - Forum software by © MyBB