Codeigniter从选择下拉列表

时间:2017-04-28 15:25:46

标签: php codeigniter

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

这是我到目前为止所做的:

view(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>

controller(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();
        }
    }
}

model(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

我尝试过很多东西,但我无法弄清楚我是如何让它正常工作的。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

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

您可以使用:

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

然后将其传递给函数

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

答案 1 :(得分:0)

更改

 $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);
}

答案 2 :(得分:0)

查看

<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);
}