Welcome Guest, Not a member yet? Register   Sign In
Form insert into DB prblem
#1

[eluser]mirag3[/eluser]
Hi, i have this code but it doesnt insert query into db or redirect to congrats_view. Alltime its getting only index function. Where is the problem? Thanks..

Controller
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Add_entry extends CI_Controller {
        
        function __construct()
{
  parent::__construct();
  $this->load->model('add_entry_model');
}
  
  
        function index()
        {      
                $this->load->view('add_entry_view');
        }

        function add()
        {
   $this->form_validation->set_rules('name', 'Name', 'required');
   $this->form_validation->set_rules('email', 'Email', 'required');

    if($this->form_validation->run() == FALSE)
     {
      redirect('/add_entry/index');
     }
     else
     {
      $this->add_entry_model->add_entry();
      //redirect('/add-entry/congrats');
     }

        }

        public function congrats()
   {
    $this->load->view('congrats_view');
   }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>

Model
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Add_entry_model extends CI_Model {

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

function add_entry()
{
  $data = array(
   'id' => '',
   'name' => $this->input->post('name'),
   'email' => $this->input->post('email')
   );

  $this->db->insert('entries',$data);
}

}
?>

View
Code:
<?php echo form_open("add_entry/add"); ?>
        <input type="text" id="name" name="name" />
        <input type="text" id="email" name="email" />
        <input type="submit" />
        <?php echo form_close(); ?>
#2

[eluser]FuzzyJared[/eluser]
First validate that it is passing form validation.

If it isn't then it will never get through to being placed in the database.

If it is getting through validation make sure the post fields are getting through to your model.

I didn't not see that you added the validation library so you must be doing that in the global level.




Theme © iAndrew 2016 - Forum software by © MyBB