• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
To connect to a database with controller

Hello everyone.

I've got a form and the "action" of it should be pointed to a controller, if I've understood it correctly. What am I supposed to type into that controller? I'm extremely confused. I've read the guide but I'm still stuck.

Quote:To connect to a database with controller
All your DB stuff should be done in a model, not your controller.

If it's all a bit confusing for you, I'd highly recommend going through some of the video tutorials and code along with them, I think it's the best way to understand (and learn how to use) a MVC environment if it's new to you.


In the controller your form points to, you write code
that processes the data send by the form and display
feedback to the user about that process.

In the user guide you find a complete example how you
can do that:

Thanks for the replies guys.

I know how to create a form as I already have one. The only thing I'm not quite getting the hang of is how to connect that form to the database so that when the form is filled in, the information will be sent to the database.

That's my issue. Sorry if I wasn't clear.

Here is (very basic) how you can insert form data into a database:

// place maybe in your constructor:

// Controller - after validation passed:
$db_data = array(
'title'  => $this->input->post('title'),
'text'  => $this->input->post('text'),
// ...



// Model

class Your_model extends CI_Model
  * The class constructer
public function __construct()

  * save data
public function add_entry($data)
  if ($this->db->insert('db_table_name', $data))
   return $this->db->insert_id();
   return FALSE;

Adjust $db_data coresponding to your table columns and input fields (name attr).

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.