首页 > 解决方案 > 使用 Codeigniter 将带有会话数据的数据插入数据库

问题描述

我在解决这个问题时遇到了麻烦,我想使用输入函数保存搜索到的数据并将其 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>

这是错误响应.. 这是错误 希望社区中的任何人都可以帮助我,。

标签: phphtmlsql-serverbootstrap-modalcodeigniter-3

解决方案


因为您的控制器 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用你的参数改变。

这里是文档


推荐阅读