我有一个应用程序,用户可以从 html selet 中选择多个项目,并且此选择需要存储为一个 id 行,
例如:
time_id time_name values
1 common x,y,z
下面是我的 PHP Controller ,
$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>
有了这个,我的多选值存储为 x、y、z 或 1、2、4 等...
但我想创建多对多链接表,我将在每个不同的行中存储 x、y、z 的值而不是逗号分隔值,
如何在 mysql 表中插入多个选定的值?
谢谢,
最佳答案
不确定我是否完全理解您的问题。看看这是否有帮助:
1) 首先,创建一个新表来存储将包含所选值的值。
2) 这个表的主键是什么?是time_id吗?如果它没有 PK,则创建一个。
3) 在刚刚创建的新表中,添加一个引用第一个现有表的主键的外键。还要使该字段/列不为空。
4) 在刚刚创建的表中根据需要添加尽可能多的行(根据选择的值的数量)。不要忘记使用 FK 将它们链接回第一个表中的正确行。
这就是您要实现的目标吗?
关于php - 将多个选择值从 html 插入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30010336/