2

I may well be missing something quite obvious, but I'm still a bit lost. The actual database interaction seems to be the catch. I mean, the table is created on installation, but actually sending the information doesn't seem to work as it triggers the validation regardless of what I enter. If I turn off the field requirements, the database fails to receive the information entirely.

From the controller:

class Admin extends Admin_Controller {

private $show_validation_rules = array(
    array(
        'field' => 'date',
        'label' => 'Date',
        'rules' => 'trim|max_length[100]|required'
    ),
    array(
        'field' => 'location',
        'label' => 'Location',
        'rules' => 'trim|max_length[300]'
    ),
    array(
        'field' => 'support',
        'label' => 'Support',
        'rules' => 'trim|required'
    )

);

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

    $this->load->model('shows_m');
    $this->load->library('form_validation');
    $this->lang->load('shows');
    $this->load->helper('html');

    $this->template->set_partial('shortcuts', 'admin/partials/shortcuts');
}

public function index()
{
    $view_data = array();
    $view_data['shows'] = $this->shows_m->get_all();
    $this->template->build('admin/index', $view_data);
}

public function create()
{

    $shows = $this->shows_m->get_all();

    $this->form_validation->set_rules($this->show_validation_rules);

    if ( $this->form_validation->run() )
    {
        if ($this->shows_m->insert_show($this->input->post()))
        {
            $this->session->set_flashdata('success', lang('shows.create_success'));
            redirect('admin/shows/index');
        } else {
            $this->session->set_flashdata('error', lang('shows.create_error'));
            redirect('admin/shows/create');
        }
    }

    foreach($this->show_validation_rules as $rule)
    {
        $shows[$rule['field']] = $this->input->post($rule['field']);
    }
    $view_data = array(
            'shows' => $shows
        );
    $this->template->build('admin/create', $view_data);
}

public function edit($id)
{
    $this->form_validation->set_rules($this->show_validation_rules);

    $show = $this->shows_m->get($id);

    if ( empty($show) )
    {
        $this->session->set_flashdata('error', lang('shows.exists_error'));
        redirect('admin/shows');
    }

    if ( $this->form_validation->run() )
    {

        if ( $this->shows_m->update_entry($id, $this->input->post()) === TRUE )
        {
            if ( isset($this->input->post()['delete']) )
            {
                $this->session->set_flashdata('success', lang('shows.delete_success'));
                redirect('admin/shows/');
            }
            else
            {
                $this->session->set_flashdata('success', lang('shows.update_success'));
                redirect('admin/shows/edit/' . $id);
            }
        } else {
            if ( isset($this->input->post()['delete']) )
            {
                $this->session->set_flashdata('error', lang('shows.delete_error'));
                redirect('admin/shows/edit/' . $id);
            }
            else
            {
                $this->session->set_flashdata('error', lang('shows.update_error'));
                redirect('admin/shows/edit/' . $id);
            }
        }
    }

    foreach($this->show_validation_rules as $rule)
    {
        if ($this->input->post($rule['field']))
        {   
            $show[$rule['field']] = $this->input->post($rule['field']);
        }
    }
    $view_data = array(
            'shows' => $show
        );
    $this->template->build('admin/edit', $view_data);
}

public function delete($id = NULL)
{
    $id_array = array();

    if ( $this->input->post() )
    {
        $id_array = $this->input->post()['action_to'];
    }
    else
    {
        if ( $id !== NULL )
        {
            $id_array[0] = $id;
        }
    }

    if ( empty($id_array) )
    {
        $this->session->set_flashdata('error', lang('shows.id_error'));
        redirect('admin/shows');
    }

    foreach ( $id_array as $id)
    {

        $show = $this->shows_m->get($id);

        if ( !empty($show) )
        {

            if ( $this->shows_m->delete($id) == FALSE )
            {
                $this->session->set_flashdata('error', lang('shows.delete_error'));
                redirect('admin/shows');
            }
        }
    }

    $this->session->set_flashdata('success', lang('shows.delete_success'));
    redirect('admin/shows');
}

}

From the details.php file

public function install()
{
    $this->dbforge->drop_table('shows');
    $shows = "
        CREATE TABLE ".$this->db->dbprefix('shows')." (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `date` varchar(100) NOT NULL,
          `location` varchar(300) NOT NULL,
          `support` text,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    ";

    if($this->db->query($shows))
    {
        return TRUE;
    }
}

The intent of the module is, in short to output the date of the show, the location and any supporting bands and other info by inserting a tag produced by the module in to the pertinent page on the site.

I'm inclined to think it should be quite simple and that I'm missing something obvious, but who knows. If I am, feel free to yell at me, I'd appreciate it.

Edit:

Code for the model

class Shows_m extends MY_Model {

public function get_all($limit = NULL)
{
    $this->db->order_by("id", "desc");
    if (isset($limit)){ $this->db->limit($limit); }
    $results = $this->db->get('shows');
    $result = $results->result_array();
    return $result;
}


public function get($id)
{
    $results = $this->db->get_where('shows', array('id' => $id));
    $result = $results->row_array();
    return $result;
}


public function insert_show($input)
{

    $to_insert = array(
        'date' => $input['date'],
        'location' => $input['location'],
        'support' => $input['support']
    );


    if ($this->db->insert('shows',$to_insert))
    {
        return TRUE;
    } else {
        return FALSE;
    }
}


public function update_entry($id, $input)
{
    $new_data = array(
        'date' => $input['date'],
        'location' => $input['location'],
        'support' => $input['support']
    );

    if (isset ($input['delete']) )
    {
        if($this->delete($id))
        {
            return TRUE;
        } else {
            return FALSE;
        }
    } else {

        $this->db->where('id', $id);
        if ($this->db->update('shows', $new_data))
        {
            return TRUE;    
        } else {
            return FALSE;    
        }
    }
}


public function delete($id)
{
    if ($this->db->delete('shows', array('id' => $id)))
    {
        return TRUE;    
    } else {
        return FALSE;    
    }
}
}
  • Can you copy/paste the rest of the code? Like you have on the CI forums? Also might help to see the HTML generated on the page you're having an issue with. – stormdrain Aug 30 '11 at 19:46
  • Editing the post to change $_POST to $this->input->post() has actually resulted in the white screen of death...and error reporting in CodeIgniter isn't indicating the possible problem. Also, added full code for admin controller and the pertinent model. –  Sep 02 '11 at 07:30

1 Answers1

0

It might be your insert in the model

if ($this->db->insert('shows',$to_insert))
    {
        return TRUE;
    } else {
        return FALSE;
    }

Try instead:

$this->db->insert('shows',$to_insert);
$query_result = $this->db->insert_id();
if($query_result){
    return TRUE;
}else{
    return FALSE;
}

I don't think insert returns anything.

At any rate, it doesn't sound like validation is the problem. The query isn't getting to the db. If the above isn't the issue, try just echoing out the POST data; make sure it's getting to the model (make sure the HTML is as expected--input names and such).

stormdrain
  • 7,915
  • 4
  • 37
  • 76