在解决此问题时遇到麻烦,我想使用输入功能保存搜索到的数据,并将其ID传递给控制器,但是我对此有疑问,
这是我的控制器:
function add($id){
$this->point_m->addStudentSection($id);
redirect('admins_view/points/index');
}
这是我的模特:
function addStudentSection($id){
$arr['section_id'] = $this->session->userdata('section_id');
$arr['dateadded'] = $this->input->post('dateadded');
$arr['schoolyear'] = $this->input->post('schoolyear');
$arr['semester'] = $this->input->post('semester');
$arr['point'] = $this->input->post('point');
$this->db->where('student_id',$id);
$this->db->insert('section_student',$arr);
}
如果上面的模型是错误的,这是我也在使用的其他模型函数,我也在数据库中使用sqlsrv。
function addStudentSection($id){
$arr['section_id'] = $this->session->userdata('section_id');
$arr['dateadded'] = $this->input->post('dateadded');
$arr['schoolyear'] = $this->input->post('schoolyear');
$arr['semester'] = $this->input->post('semester');
$arr['point'] = $this->input->post('point');
$query = $this->db->query("
INSERT INTO [dbo].[section_student]
([student_id]
,[section_id]
,[dateadded]
,[schoolyear]
,[semester]
,[point])
VALUES
('$id'
,'$section_id'
,'$dateadded'
,'$schoolyear'
,'$semester'
,'$point')
");
}
因此在此模型中,我还要使用我的section_id,即会话
这是我的观点:
<table class="table">
<tr>
<th>ID Number</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Lastname</th>
<th>Total Points</th>
<th>Action</th>
</tr>
<?php
foreach ($pt as $p) {
?>
<tr>
<!-- echo query with join -->
<td><?php echo $p->idnumber ?></td>
<td><?php echo $p->firstname ?></td>
<td><?php echo $p->middlename ?></td>
<td><?php echo $p->lastname ?></td>
<td><?php echo $p->point ?></td>
<td>
<a href="<?php echo site_url('admins/point/add').'/'.'$p->id';?>" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Add</a>
<a href="#" class="btn btn-danger">Redeem</a>
</td>
</tr>
<?php
}
?>
</table>
此表中包含我显示功能中所有搜索和检索的数据,您可以看到要切换的按钮是模态。 这是我的模态:
<!-- Modal -->
<form method="post" action="<?php echo base_url();?>admins/point/add">
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Point/s</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<label>Date</label>
<input type="date" name="dateadded" class="form-control">
<label>School year</label>
<select name="schoolyear" class="form-control">
<option value="2018-2019">2018-2019</option>
<option value="2019-2020">2019-2020</option>
</select>
<label>Semester</label>
<select name="semester" class="form-control">
<option value="1st">First Semester</option>
<option value="2nd">Second Semester</option>
<option value="summer">Summer</option>
</select>
<label>Points</label>
<input type="text" name="point" class="form-control">
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</form>
,这是错误响应。 this is the error 希望社区中的任何人都能帮助我。
答案 0 :(得分:1)
因为控制器的add($ id)期望值为1,所以在模态视图中无法设置如下形式的动作:
<form method="post" action="<?php echo base_url();?>admins/point/add">
将表单操作更改为此:
<form method="post" action="<?php echo base_url();?>admins/point/add/$id">
使用您的参数更改$id
。
docs。
答案 1 :(得分:0)
您应该在视图的表单操作末尾添加ID。
或者您可以将其从控制器的功能中删除并将其发布为隐藏输入
因此将您的视图更改为
<form method="post" action="<?php echo base_url();?>admins/point/add/$id">
或转到您的控制器并将其更改为
function add($id = null){
if($id == null){
$id = $this->input->post('id');
}
}
然后在这样的表单关闭标记之前向视图添加新的隐藏输入
<?php echo form_hidden('id',$the_id);?>