I am using multiselect jquery plugin for dropdown which allows multiple values to be selected with check boxes.. I am trying to store those values into database.. In view if i use [] symbol in name attribute of input tag, it gives error .. if i don't use that symbol, only one value will be stored. since multiple values are selected here it is an array so this [] symbol has to be used here.... please help me solve this...
my view is
<div class="col-md-8 col-lg-offset-2">
<label>Select User Name <i class="required"> * </i> </label>
<select name="user_id" class="form-control">
<?php foreach($user->result() as $link){ ?>
<option value="<?php echo $link->user_id ?>"><?php echo $link->username ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-8 col-lg-offset-2">
<label>Select Course Name <i class="required"> * </i> </label>
<select name="course_id[]" class="form-control" multiple id="course_id">
<?php foreach($course->result() as $link){ ?>
<option value="<?php echo $link->id ?>"><?php echo $link->title ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-8 col-lg-offset-2">
<label>Download Access<i class="required"> * </i> </label>
<select class="form-control" name="access" id="access">
<option value=''>--Select Access Type--</option>
<option value="yes" <?php echo set_select('access', 'yes'); ?> >Yes</option>
<option value="no" <?php echo set_select('access', 'no'); ?> > No</option>
</select>
</div>
my controller is
function add() {
$this - > _add_set_rules();
$data = array();
if ($this - > form_validation - > run() == FALSE) {
$data['user'] = $this - > user_course_model - > get_user();
$data['course'] = $this - > user_course_model - > get_course();
$this - > load - > view('permission/user_course_view', array('left_sidebar' => 'sidebar/left_sidebar', 'right_sidebar' => 'sidebar/right_sidebar'), $data, TRUE);
} else {
//Save and redirect..
$data = $this - > input - > post();
$inputs_array = array('course_id' => $data['course_id'],
'user_id' => $data['user_id'],
'download_access' => $data['access']
);
$inserted_id = $this - > user_course_model - > save_user_course($inputs_array);
// print_r($inserted_id);
// exit();
if ($inserted_id) {
$this - > session - > set_flashdata('msg', "Access created successfully..");
redirect(base_url().
'user_course', 'refresh');
} else {
$this - > session - > set_flashdata('errormsg', "Something went wrong please contact admin.");
redirect(base_url().
'user_course', 'refresh');
}
}
}
function _add_set_rules() {
$this - > form_validation - > set_rules('user_id', 'User Name', 'trim||xss_clean|required');
$this - > form_validation - > set_rules('access', ' Access', 'trim||xss_clean|required');
$this - > form_validation - > set_rules('course_id[]', ' Course Title', 'trim||xss_clean|required');
}
my model is
function save_user_course($inputs_array) {
$this - > db - > insert('user_course', $inputs_array);
return ($this - > db - > affected_rows() != 1) ? false : $this - > db - > insert_id();
}
Table Structure
id course_id user_id download_access
答案 0 :(得分:0)
Check the following line:
$inputs_array = array('course_id' => $data['course_id'],
'user_id' => $data['user_id'],
'download_access' => $data['access']
);
$inserted_id = $this - > user_course_model - > save_user_course($inputs_array);
here $data['course_id']
is an array, and you cannot add an array directly to the database table.