【问题标题】:Why are session Ids in my table changing to 0?为什么我的表中的会话 ID 变为 0?
【发布时间】:2020-04-30 20:34:03
【问题描述】:

我有一个用于提交学生分数的表格,该表格应该捕获会话 ID(会话 ID 用于显示学校当前正在进行的会话,例如 2018/2019 会话、2019/2020 会话......)

session table

在数据库的scores表中,session id被自动捕获并插入。目前,会话 id 为 14。

session id

但是,由于未知的原因,插入了 0 而不是 14。这导致了很多问题

  1. 在添加分数表单中,仅当会话 id 为 14 时才会重新填充分数值。现在,由于它显示为 0,因此值框保持为 0

  2. 我也相信它会导致重复问题。在表单中的编辑过程中,所有学生的分数都会重复。

控制器

function assigngradeAction() 
{


            for($i=0; $i<count($this->input->post('number')); $i++)
                {

                    $data[]=array(
                         'section_id' => $this->input->post('section_id'),
                        'subject_id' => $this->input->post('subject_id'),
                        'class_id' => $this->input->post('class_id')[$i],
                        'student_id' => $this->input->post('student_id')[$i],
                        'session_id' => $this->input->post('session_id'),
                        'ca1' => $this->input->post('ca1')[$i],
                        'ca2' => $this->input->post('ca2')[$i],
                        'ca3' => $this->input->post('ca3')[$i],
                        'ca4' => $this->input->post('ca4')[$i],
                        'project' => $this->input->post('project')[$i],
                        'affective' => $this->input->post('affective')[$i],
                        'psychomotor' => $this->input->post('psychomotor')[$i],
                        'exam'=> $this->input->post('exam')[$i],
                        'tot_score'=> $this->input->post('ca1')[$i] + $this->input->post('ca2')[$i] + $this->input->post('ca3')[$i] + $this->input->post('ca4')[$i] + $this->input->post('project')[$i] + $this->input->post('affective')[$i] + $this->input->post('psychomotor')[$i] + $this->input->post('exam')[$i],
                    );

                }




                 $inserted = $this->primary_model->add1($data);
                 if($inserted > 0)
                 {
                     $this->session->set_flashdata('msg', '<div class="alert alert-success">Grade Added successfully</div>');
                     //Echo back success json
                     redirect('admin/primary/index');
                  }           

    }

模型

public function add1($data)
    {

        // for each record insert
        foreach($data as $studentScore){
            // get score
            $subjectId = $studentScore['subject_id'];
            $classId = $studentScore['class_id'];
            $sessionId = $studentScore['session_id'];
            $sectionId = $studentScore['section_id'];
            $studentId = $studentScore['student_id'];
            $score = $this->get_student_score($subjectId,$sessionId,$sectionId,$classId,$studentId); 
            $studentScore['modified_at'] = date("Y-m-d H:i:s");
            if(empty($score->id)){
                $this->db->insert('scores_primary', $studentScore);
            }else{
                $this->db->where('id', $score->id);
                $this->db->update('scores_primary', $studentScore);
            }
           // var_dump($studentScore, "\n >>>>>>>>", $score);

        }
    //   $this->db->insert_batch('scores_primary', $data);
    //   var_dump($this->db->error(), $_SERVER['SERVER_ADDR'] );
    //   $str = $this->db->last_query();

    // echo "<pre>";
    // print_r($str);
       return true; 
    }

查看

<?php }elseif($class_id >= 15 && $class_id <= 17){ ?>
                            <form action="<?php echo site_url('admin/primary/assigngradeAction') ?>" method="POST" id="formSubjectTeacher">
                               <?php echo $this->customlib->getCSRF(); ?>
                               <div class="row">
                                        <div class="col-lg-3">
                                        <input type="hidden" name="class" value="<?php echo $class_id; ?>">
                                            <input type="hidden" name="subject_id" value="<?php echo $subject_id; ?>">
                                        </div>
                                        <div class="col-lg-4">

                                        <h4><strong><?php echo $session_name; ?></strong></h4>
                                        </div>
                                    </div>
                                    <br>
                                    <hr>

                                <?php foreach($students as $student){ ?>
                                    <div class="row">
                                        <div class="col-lg-3">
                                        <div class="form-group">
                                                <label>Student Name</label>
                                                <input type="hidden" name="number[]"  value="">
                                                <input type="hidden" name="section_id"  value="<?php echo $section_id; ?>">
                                                <input type="hidden" name="session_id"  value="<?php echo $student->session_id; ?>">
                                                <input type="hidden" name="student_id[]"  value="<?php echo $student->student_id; ?>">
                                                <input type="hidden" name="class_id[]" value="<?php echo $class_id; ?>">
                                                <input type="text" value="<?php echo $CI->GetStudentNameWithID($student->student_id); ?>" class="form-control "  readonly>
                                            </div>
                                        </div>
                                        <div class="col-lg-1">
                                            <label>ca1 </label>
                                            <input type="hidden" name="session_id" value="<?php echo $sessionID; ?>">
                                            <input type="number" name="ca1[]" min="0" max="10" class="form-control input-sm rounded-0" value="<?php echo $student->scores? $student->scores->ca1: 0; ?>">
                                        </div>
                                        <div class="col-lg-1" id="t2">
                                            <label>ca2</label>
                                            <input type="number" name="ca2[]" min="0" max="10" class="form-control input-sm rounded-0" value="<?php echo $student->scores? $student->scores->ca2: 0; ?>">
                                        </div>
                                        <div class="col-lg-1" id="assg">
                                            <label>ca3</label>
                                            <input type="number" name="ca3[]" min="0" max="10" class="form-control input-sm rounded-0" value="<?php echo $student->scores? $student->scores->ca3: 0; ?>">
                                        </div>
                                        <div class="col-lg-1" id="exam">
                                            <label>Exam</label>
                                            <input type="number" name="exam[]" min="0" max="70" class="form-control input-sm rounded-0" value="<?php echo $student->scores? $student->scores->exam: 0; ?>">
                                        </div>
                                        <div class="col-lg-1">
                                            <label>Total</label>
                                            <output class="result"></output>
                                        </div>
                                    </div>

                              <?php } ?>

【问题讨论】:

  • 你有没有设置主键和自增的id?
  • @Vickel 是的,我愿意

标签: php mysql sql codeigniter


【解决方案1】:

在您的viewform 中,您有两个名为session_idinput fields ↓↓

  1. &lt;input type="hidden" name="session_id" value="&lt;?php echo $student-&gt;session_id; ?&gt;"&gt;

  2. &lt;input type="hidden" name="session_id" value="&lt;?php echo $sessionID; ?&gt;"&gt;

重复的字段可能会导致问题,请检查哪个字段正确 价值并丢弃另一个。如果两个字段都有 错误的值然后检查值在哪里被初始化等等 在。这就是您在 PHP 中调试的方式。

【讨论】:

    猜你喜欢
    • 2011-05-04
    • 2014-11-04
    • 2011-07-19
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多