Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

726
Visualizações
Codeigniter Updating database data from a select dropdown

I'm trying to update database names from a select dropdown.

This is what I have so far:

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

I'm getting this error:

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

I've tried lots of things but i can't figure it out how i can get it to work properly. Any help would be appreciated.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The error you see stems from an incorrectly formatted variable being passed to update the table. In the editExam model function the $id variable is passed to update the column but it does not contain an array format.

You can use:

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

then pass it to the function

function editExam($id, $data2){
    $this->db->where('exam_id', $id);
    $this->db->update('exam', $data2);
}
over 4 years ago · Santiago Trujillo Relatório

0

change

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

to

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

then change the editExam function

function editExam($exam_id, $data)
{
    $this->db->where('exam_id', $exam_id);
    $this->db->update('exam', $data);
}
over 4 years ago · Santiago Trujillo Relatório

0

View

<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

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

Model

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);
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda