CodeIgniter -将从下拉列表中选择的值插入到数据库[重复]

wd2eg0qa  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(130)

此问题在此处已有答案

How I Get selected value when i edit data [codeigniter](1个答案)
5个月前关闭。
我需要在数据库的rem1列中添加备注,如“good”、“very good”和“improvinging”。无论在下拉列表中选择哪个备注,都应该保存在表中。但是,当我执行下面的代码时,得到一个空值。我做错了什么?
这是我的视图代码:

<div>
  <select class="form-control" name="remark">
    <option value="" disabled selected>Choose option</option>
    <option name="exam_group_student_remark_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="Good">Good</option>
    <option name="exam_group_student_remark_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="Very Good">Very Good</option>
    <option name="exam_group_student_remark_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="Improving">Improving</option>
  </select>
</div>

控制器:

if (!empty($exam_group_student_id)) {
 foreach ($exam_group_student_id as $exam_group_student_key => $exam_group_student_value) {

 $array = array(
 'exam_group_class_batch_exam_subject_id' => $this->input->post('exam_group_class_batch_exam_subject_id'),
 'exam_group_class_batch_exam_student_id' => $exam_group_student_value,
 'rem1' => $this->input->post('exam_group_student_remark_' . $exam_group_student_value),
 
 );
 $insert_array[] = $array;
 }
 }
 $this->examgroupstudent_model->add_result($insert_array);
mtb9vblg

mtb9vblg1#

使用如下代码,在php中,取'select'标记中的name属性,而不是'option'标记中的name属性

<div>
  <select class="form-control" name="remark">
    <option value="" disabled selected>Choose option</option>
    <option value="Good">Good</option>
    <option value="Very Good">Very Good</option>
    <option value="Improving">Improving</option>
  </select>
</div>
//controller

if (!empty($exam_group_student_id)) {
 foreach ($exam_group_student_id as $exam_group_student_key => $exam_group_student_value) {

 $array = array(
 'exam_group_class_batch_exam_subject_id' => $this->input->post('exam_group_class_batch_exam_subject_id'),
 'exam_group_class_batch_exam_student_id' => $exam_group_student_value,
 'rem1' => $this->input->post('remark'),
 
 );
 $insert_array[] = $array;
 }
 }
 $this->examgroupstudent_model->add_result($insert_array);

相关问题