Edit form does not display loaded data

Asked

Viewed 46 times

0

I have been trying for some time to make a form in Codeigniter 3 for editing data to display in your fields the values of each column of the selected record. The operation update is working normally, but as the fields come blank, I need to type everything again.

Down with my model MY_Model.php:

<?php

class MY_Model extends CI_Model
{
    public $table = '';
    protected $primary_key = 'id';

    public function __construct()
    {
        parent::__construct();

        $this->load->database();
        $this->load->helper('inflector');
    }

    public function get($id)
    {
        return $this->db->get_where($this->table, array($this->primary_key = $id))->row();
    }

    public function select()
    {
        $data = array();

        $this->db->from($this->table);
        $this->db->order_by($this->primary_key, 'DESC');
        $query = $this->db->get();

        foreach ($query->result_array() as $row) {
            $data[] = $row;
        }

        $query->free_result();

        return $data;
    }

    public function insert($data)
    {
        $data['date_created'] = $data['date_updated'] = date('Y-m-d');
        $data['created_from_ip'] = $data['updated_from_ip'] = $this->input->ip_address();

        if ($this->db->insert($this->table, $data)) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    public function update($data, $id)
    {
        $data['date_updated'] = date('Y-m-d');
        $data['updated_from_ip'] = $this->input->ip_address();

        $this->db->where($this->primary_key, $id);

        return $this->db->update($this->table, $data);
    }

    public function delete($id)
    {
        $this->db->where($this->primary_key, $id);

        return $this->db->delete($this->table);
    }

    public function count_all()
    {
        return $this->db->count_all($this->table);
    }
}

Here the function edit of control Customers:

public function edit($id)
{
    if ($this->input->post()) {
        $data['name'] = $this->input->post('name');
        $data['email'] = $this->input->post('email');
        $data['address'] = $this->input->post('address');

        $this->Customers_model->update($data, $id);
        $this->session->set_flashdata('message', 'Cliente \'' . $data['name'] . '\' alterado');

        redirect('/admin/customers', 'refresh');
    }

    $data['customer'] = $this->Customers_model->get($id);
    $data['page'] = $this->config->item('admin_template_dir_admin') . 'edit_customers';
    $data['module'] = 'admin';

    $this->load->view($this->_container, $data);
}

I’ve tried a lot to display the data for editing. As it is now, making a var_dump($customer) just shows NULL. How to proceed?

  • If update screen gets the blank fields, it is a sign that select went wrong or did not return anything from that id.

  • 1

    I solved. Missing a '>' in the function get of MY_Model

  • Creates an answer :)

  • Ready! Looks like bruncadeira.

1 answer

1


The function get() is with a > missing.

return $this->db->get_where($this->table, array($this->primary_key = $id))->row();

Stay like this

return $this->db->get_where($this->table, array($this->primary_key => $id))->row();

I can hardly believe.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.