繁体   English   中英

Codeigniter从选择下拉列表更新数据库数据

[英]Codeigniter Updating database data from a select dropdown

我正在尝试从选择下拉列表中更新数据库名称。

这是我到目前为止的内容:

视图(exammenu_view.php)

<form name='selectexam' action="<?php echo base_url() . "index.php/exam/select/";?>" method='post'>   
<div class="form-group">
   <select class="form-control" name="exam_id">
    <?php 
    foreach($exams as $row)
    { 
      echo '<option value="'.$row->exam_id.'">'.$row->examname.'</option>';
    }
    ?>
    </select>
</div>
    <label>Examen wijzigen</label>
    <div class="form-group"><input type="text" name="update" class="form-control" placeholder="Examennaam"></div>
<div class="form-group">
    <input type="submit" name="sbm" value="Wijzigen" class="btn btn-info" />
    <input type="submit" name="sbm" value="Verwijderen" class="btn btn-info" />
</div>
</form>

控制器(exam.php)

function select() {
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if($this->input->post('sbm') == 'Verwijderen') { 
        $exam_id = $_POST['exam_id'];   
        $this->exam_model->removeExam($exam_id);

        if(!empty($exam_id)){
            $this->session->set_flashdata('del','<div class="alert alert-success text-center">Examen verwijderd.</div>'); }
        elseif(empty($exam_id)){
            $this->session->set_flashdata('del','<div class="alert alert-warning text-center">Er zijn geen examens gevonden om te verwijderen.</div>');  
        }
        redirect('/exam/');
    }

        elseif($this->input->post('sbm') == "Wijzigen") {
            $data = $this->input->post('dname');
            $this->exam_model->editExam($data);
            $this->getExamName();
        }
    }
}

模型(exam_model.php)

function getExamName(){
    $query = $this->db->query('SELECT exam_id, examname FROM exam');
    return $query->result();
}  

function removeExam($id){
    $this->db->where('exam_id', $id);
    $this->db->delete('exam');
} 

function editExam($id){
    $this->db->where('exam_id', $id);
    $this->db->update('exam', $id);
} 

我收到此错误:

A Database Error Occurred
You must use the "set" method to update an entry.
Filename: models/Exam_model.php

我已经尝试了很多东西,但我不知道如何使它正常工作。 任何帮助,将不胜感激。

您看到的错误源于传递格式错误的变量以更新表。 在editExam模型函数中,传递了$ id变量以更新列,但该列不包含数组格式。

您可以使用:

$data2 = array('table_column_name' => 'dname');

然后将其传递给函数

function editExam($id, $data2){
    $this->db->where('exam_id', $id);
    $this->db->update('exam', $data2);
}

更改

 $data = $this->input->post('dname');
 $this->exam_model->editExam($data);

$data = $this->input->post('update');
$this->exam_model->editExam($data,$exam_id);

然后更改editExam函数

function editExam($exam_id, $data)
{
    $this->db->where('exam_id', $exam_id);
    $this->db->update('exam', $data);
}

视图

<form name='selectexam' action="<?php echo base_url() . "index.php/exam/select/";?>" method='post'>   
    <div class="form-group">
        <select class="form-control" name="exam_id">
        <?php 
        foreach($exams as $row)
        { 
        echo '<option value="'.$row->exam_id.'">'.$row->examname.'</option>';
        }
        ?>
        </select>
    </div>
    <label>Examen wijzigen</label>
    <div class="form-group"><input type="text" name="update" class="form-control" placeholder="Examennaam"></div>
    <div class="form-group">
        <input type="submit" name="sbm" value="Wijzigen" class="btn btn-info" />
        <input type="submit" name="sbm" value="Verwijderen" class="btn btn-info" />
</div>
</form>

调节器

function select() {
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if($this->input->post('sbm') == 'Verwijderen') { 
    $exam_id = $_POST['exam_id'];   
    $this->exam_model->removeExam($exam_id);

    if(!empty($exam_id)){
        $this->session->set_flashdata('del','<div class="alert alert-success text-center">Examen verwijderd.</div>'); }
    elseif(empty($exam_id)){
        $this->session->set_flashdata('del','<div class="alert alert-warning text-center">Er zijn geen examens gevonden om te verwijderen.</div>');  
    }
    redirect('/exam/');
}

    elseif($this->input->post('sbm') == "Wijzigen") {
        $data2 = array(
            'examname' => $this->input->post ('update'),
        );
        $id = $_POST['exam_id'];
        $this->exam_model->editExam($id, $data2);
        $this->exam_model->getExamName();
    }
    redirect('/exam/');
    }
}

模型

function getExamName(){
    $query = $this->db->query('SELECT exam_id, examname FROM exam');
    return $query->result();
}  

function removeExam($id){
    $this->db->where('exam_id', $id);
    $this->db->delete('exam');
} 

function editExam($id, $data2){
    $this->db->where('exam_id', $id);
    $this->db->update('exam', $data2);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM