1
我有应用程序,其中用户可以从HTML selet选择多个项目,这个选择需要保存为一个ID行,插入多个选择值从HTML到MySQL数据库
例:
time_id time_name values
1 common x,y,z
下面是我的PHP控制器,
$time_data=array(
'time_name'=>$this->input->post('time_name'),
'time_days'=>$this->input->post('time_days'),
'time_hours'=>$this->input->post('time_hours'),
'time_minutes'=>$this->input->post('time_minutes'),
'time_start'=> implode(",", $this->input->post('time_start')),
'time_end'=>implode(",", $this->input->post('time_end')),
'time_department'=>implode(",", $this->input->post('time_department')),
'time_timecategory'=>$this->input->post('time_timecategory'),
'time_searchwords'=>$this->input->post('time_searchwords'),
'timecreated_time' =>date("Y-m-d H:i:s")
);
//Add starts
if($this->form_validation->run() !== FALSE) {
$result = $this->model_admin->updatetime($time_data);
if(!$result) {
$content = $this->model_admin->LastEntrytime();
echo json_encode($content);
}
}
else {
echo json_encode(array('cival'=>0, 'val_message' => validation_errors()));
}
下面是我的HTML,其中用户可以选择从下拉菜单中的多个项目,
<div class="col-md-9">
<select id="time_department" name="time_department[]" class="form-control select2" multiple>
<?php
foreach($departments_array as $department) { ?>
<option value="<?php echo $department["department_id"];?>"><?php echo $department["department_name"]?></option>
<?php
} ?>
</select>
</div>
有了这个我multiselected值存储为X,Y,Z或1,2,4等..
但我想创造出许多一对多的链接表中,我将存储值x,y,z在每个不同的行,而不是逗号分隔值,
我怎么可以在多个表中插入多个选定的值在MySQL中?
谢谢,
是几乎相同,time_id是PK,使得更清楚,我需要创建一行time_id将是1,此行有time_name = test和time_values =好,坏,soso,blabla。或者可能像2,4,5,10这是其他表(FK)的ID,现在与2,4,5存储不是好主意,所以我需要为新表中的每个多选择值创建行?其结构像1,好,2 - 1,坏,4 - 1,索索,5 - 1,blabla - 10这样,而不是存储为逗号分隔,我会存储在表中,如何做到这一点? – rjcode
像这样,http://stackoverflow.com/questions/4804841/mysql-storing-multiple-value-in-single-column-from-another-table,这在列中有ids,但将有FKs +字符串作为好, – rjcode