php - Codeigniter can't update the form when click submit

933

I'm having trouble with codeigniter, when I click on submit it won't update the data.

I'm creating the super admin so it can delete or update the normal admin data.

Here's my code:

controller

<?php

class Superadmin extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        //$this->session->userdata('is_login') == FALSE || redirect('admin/Login_admin');
        if($this->session->userdata('token'))
        $this->session->unset_userdata('token');
        $this->load->model('admin/Admin_m');
    }

    public function index()
    {
        if(isset($this->session->id)=='') 
        {
            redirect('admin/Login');
        }

        $data['content'] = 'page/admin/superadmin';
        $data['admin'] = $this->Admin_m->get_all();
        $this->load->view('layout_admin', $data);
    }

    public function editadmin($admin_id)
    {
        if(isset($this->session->id)=='') 
        {
            redirect('admin/Login');
        }

        $data['content'] = 'page/admin/superadminedit';
        $data['admin']   = $this->Admin_m->get_admin_by_id($admin_id);

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('page/admin/superadminedit', $data);
        }
        else
        {
        $this->form_validation->set_rules('full_name', ' Full name', 'required');
        $this->form_validation->set_rules('user', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
            // kondisi untuk input data jika password kosong
            if($this->input->post('password') == ''){
                $data = 
                array(
                'full_name' => $this->input->post('full_name'),
                'user'      => $this->input->post('user'),
                'level'     => $this->input->post('level'),
                );
            }
            else
            {
            $data = 
                array(
                'full_name' => $this->input->post('full_name'),
                'user'      => $this->input->post('user'),
                'password'  => hashpassword($this->input->post('password')),
                'level'     => $this->input->post('level'),
                );
            }
                $this->Admin_m->edit_admin($admin_id,$data);
                redirect('admin/superadmin/editadmin');
        } 
    }


    public function deleteadmin($id)
    {
        if(isset($this->session->id)=='') 
        {
            redirect('admin/Login');
        }

        $this->load->model('Admin_m');

        $this->Admin_m->delete($id);

        //$this->session->set_flashdata('remove_success', 'Admin berhasil dihapus dari List');

        redirect('admin/Superadmin');
    }

}

?>

model

<?php

class Admin_m extends CI_Model
{
    public function check_akunadmin($user= '' , $password = '')
    {
        return $this->db->get_where('admin_user', 
            array('user' =>$user, 
                  'password' => hashpassword($password)
                  )
                    )->row();

    }

    public function insert($data = array())
    {
        $this->db->insert('admin_user',$data);
    }

    public function edit_admin($id, $data = array())
    {
         $this->db->where('id', $id);
         $this->db->set($data);
         $this->db->update('admin_user');
    }

    public function get_admin_by_id($admin_id)
    {
        return $this->db->get_where('admin_user', array('id' => $admin_id))->row();
    }

    public function get_all($per_page = 0, $offset = 0)
    {
        $this->db->order_by('id', 'desc');
        return $this->db->limit($per_page, $offset)->get('admin_user')->result();
    }

    public function delete($id)
    {
        $this->db->where('id', $id);
        $this->db->delete('admin_user'); 
    }

}

?>

view

after i click submit on the view page it just wont updating in the database...

what went wrong? i hope someone understand and explain it to me...

thank you so much for helping me.

618

Answer

Solution:

try this:

public function edit_admin($id, $data = array())
{
    $this->db->where('id', $id);
    $this->db->update('admin_user', $data);
}

Also, do a print_r($data);exit; right before the where to check if the model is receiving the data properly.

OFF: Let me give you a hand here... There's no need to repeat the entire array.

if($this->input->post('password') == ''){
    $data = 
    array(
        'full_name' => $this->input->post('full_name'),
        'user'      => $this->input->post('user'),
        'level'     => $this->input->post('level'),
    );
}
else
{
    $data = 
    array(
        'full_name' => $this->input->post('full_name'),
        'user'      => $this->input->post('user'),
        'password'  => hashpassword($this->input->post('password')),
        'level'     => $this->input->post('level'),
    );
}

Should be:

$data = 
array(
    'full_name' => $this->input->post('full_name'),
    'user'      => $this->input->post('user'),
    'level'     => $this->input->post('level'),
);

if(!empty($this->input->post('password'))){
    $data['password'] = hashpassword($this->input->post('password'));
}

People are also looking for solutions to the problem: php - The requested resource was not found on this server

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.