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