使用Codeigniter将具有会话数据的数据插入数据库

问题描述 投票:0回答:2

我在解决这个问题时遇到了麻烦,我想使用输入函数保存搜索到的数据并将其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">&times;</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希望社区中的任何人都可以帮助我。

php html sql-server bootstrap-modal codeigniter-3
2个回答
1
投票

因为你的控制器添加($ 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


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);?>
© www.soinside.com 2019 - 2024. All rights reserved.