Welcome Guest, Not a member yet? Register   Sign In
To connect to a database with controller
#1

[eluser]grejsimojs[/eluser]
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.
#2

[eluser]boltsabre[/eluser]
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.

http://codeigniter.com/tutorials/
#3

[eluser]LuckyFella73[/eluser]
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:
http://ellislab.com/codeigniter/user-gui...ation.html
#4

[eluser]grejsimojs[/eluser]
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.
#5

[eluser]LuckyFella73[/eluser]
Here is (very basic) how you can insert form data into a database:
Code:
<?php

// place maybe in your constructor:
$this->load->model('your_model');


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

);

$this->your_model->add_entry($db_data);


// Model

class Your_model extends CI_Model
{
/**
  * The class constructer
  *
  */
public function __construct()
{
  parent::__construct();
}



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

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




Theme © iAndrew 2016 - Forum software by © MyBB