-1

How to Save the Checkbox Value Form to MySQL in One Field, can anyone help me..enter image description here Here is my code:

function student($param1 = '') {
    if ($this->session->userdata('admin_login') != 1) {
        redirect('login', 'refresh');
    }

    $running_year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
    if ($param1 == 'create') {
        $data['ekskul_id'] = $this->input->post('ekskul_id');
        $this->db->insert('student', $data);
    }
}

this is my display code

<div class="form-group">
    <label for="field-2" class="col-sm-3 control-label">Ekstrakurikuler</label>
    <div class="col-sm-5">
        <select data-placeholder="Select" name="ekskul_id[]" multiple class="chosen-select" tabindex="8">
        <?php
            $ekstra = $this->db->get('ekstrakurikuler')->result_array();
            foreach($ekstra as $row):
        ?>
                <option value="<?php echo $row['ekskul_id'];?>"><?php echo $row['ekskul_name'];?></option>
        <?php
            endforeach;
        ?>
        </select>
    </div>
</div>
Vinay Patil
  • 736
  • 6
  • 19
Siakang
  • 21
  • 6
  • 1
    Saving multiple values in one column is a _terrible_ database design. You should read up on [database normalization](https://www.essentialsql.com/get-ready-to-learn-sql-database-normalization-explained-in-simple-english/) instead. – M. Eriksson Dec 03 '19 at 06:40
  • Terrible? ok ready, boss :D Thank you sir, for the information – Siakang Dec 03 '19 at 07:18
  • Yes. Terrible. [Here's an answer pointing out a bunch of reasons why](https://stackoverflow.com/a/3653574/2453432). – M. Eriksson Dec 03 '19 at 12:29
  • And this is very helpful and adds to my experience, thank you, sir – Siakang Dec 04 '19 at 17:59

1 Answers1

1

use below code to store multiple id in one field:

<?php 
$ekskul_id= implode(",",$_POST['ekskul_id']);
?>

In your code :

if(isset($this->input->post('ekskul_id'))){
 $data['ekskul_id'] = implode(",",$this->input->post('ekskul_id'));
}
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
BInjal Patel
  • 321
  • 3
  • 16
  • @aviboy2006 `isset` will throw an error as `isset() only works with variables as passing anything else will result in a parse error.`. So you need to do it like this `if($this->input->post('ekskul_id'))`. – Vinay Patil Dec 03 '19 at 07:17
  • @DanzMuhammad what is the datatype for the `ekskul_id ` field? – Vinay Patil Dec 03 '19 at 09:28
  • Try to print the content of the `$data` before insert statement and look for the comma-separated values in `ekskul_id` – Vinay Patil Dec 03 '19 at 09:30
  • @Vinay Patil I have searched for tutorials, but all of them are checkboxes of input type not option values, my data type is INTEGER and I want to retrieve all of that data in one field in mysql "1,2,3" – Siakang Dec 04 '19 at 17:42
  • The major problem is the datatype. As you are storing the comma separated string you need to change field data type to varchar or text. Then above code will work. – Vinay Patil Dec 04 '19 at 17:45
  • @Vinay Patil I tried using insert_batch and succeeded and entered, but the data entered into two fields in mysql. – Siakang Dec 04 '19 at 17:48
  • I have tried and changed the type to long_text but the result is NULL and not entered – Siakang Dec 04 '19 at 17:51
  • Use `$this->db->last_query()` to get the last query executed and paste it here. – Vinay Patil Dec 05 '19 at 05:12