CodeIgniter Forums
How to update multiple or single row in single query using where multiple or single i - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: How to update multiple or single row in single query using where multiple or single i (/showthread.php?tid=79091)



How to update multiple or single row in single query using where multiple or single i - edgematrix - 04-19-2021

I want to update multiple or single row in database in one query where multiple ids or single id post from the form. I couldn't find it works in update batch. I don't know how to do it. I have already searched on google on how to make it but I have no luck. Your answer is very much appreciated.

Html & AJAX Code


Code:
<form class="form-horizontal" enctype="multipart/form-data" method="POST">
   <div class="form-group">
      <label class="col-sm-3 control-label" for="name"><?php echo $this->lang->line('Select Lead Contacts'); ?>
      </label>
      <div class="col-sm-9 col-md-6 col-lg-6">
         <select id="chkveg" name="mySelect[]" class="form-control" multiple="multiple">
            <?php foreach($list_of_leads_contact as $lc) {
               ?>
            <option value="<?php echo $lc->id ?>"><?php echo $lc->first_name ?> <?php echo $lc->last_name ?></option>
            <?php } ?>
         </select>
      </div>
   </div>
   <div class="form-group">
      <label class="col-sm-3 control-label" ><?php echo $this->lang->line('Contact Group'); ?> *
      </label>
      <div class="col-sm-9 col-md-6 col-lg-6">
         <?php if(isset($group_checkbox)) echo $group_checkbox; ?>
         <span class="red">
         <?php
            if($this->session->userdata('group_type_error')==1){
                echo '<b>'.$this->lang->line('Contact Group').'</b>'.str_replace("%s","", $this->lang->line("required"));
            $this->session->unset_userdata('group_type_error');
            }
            ?>
         </span>
      </div>
   </div>
   </div>
   <button name="submit" id="submitButton" type="button" class="btn btn-primary btn-lg"><i class="fa fa-save"></i> <?php echo $this->lang->line('Save');?></button>
</form>
<script type="text/javascript">
   $j(function() {
   $( ".datepicker" ).datepicker();
   });
   
   $(function() {
    $('#submitButton').click(function() {
     var dataString = $('.form-horizontal').serialize();
   
   
     $.ajax({
         type: 'POST',
         url: base_url +'lead/insert_leads_in_groups', // your php url would go here
         data: dataString,
     }).done(function(msg) {
   
     });
   
   
    });
   });

</script>

Controller 

PHP Code:
public function insert_leads_in_groups()
{
    $user_id $this->input->post('mySelect');
    $contact_group_id $this->input->post('contact_type_id');

    foreach($user_id as $row ) {

        $data = array(
            array(
                'id' =>  $row,
                'contact_group_id' => $contact_group_id,

            ),
        );
        $this->db->update_batch('leads'$data'id');
    }




Error


Quote:
Code:
Error Number: 1054

Unknown column 'Array' in 'where clause'

UPDATE `leads` SET `contact_group_id` = CASE WHEN `id` = Array THEN
Array ELSE `contact_group_id` END WHERE `id` IN(Array)



RE: How to update multiple or single row in single query using where multiple or single i - demyr - 04-19-2021

Would this solution help you?