Welcome Guest, Not a member yet? Register   Sign In
Model/Controller Quick Question
#1

[eluser]ikim[/eluser]
Hi,

Im starting to learn how to keep all database stuff inside models and separate it from the controllers. However, Ive been looking around and I cant seem to understand how to insert data into the database.

Right now I have a small form in my view sending the form to the controller.
Code:
function insert()
{        
$this->load->model('Test');
$this->Test->insert_info();
}

and then the model has
Code:
function insert_info()
    {    
        $data['user_id'] = $_POST['user_id'];
        $data['about_me'] = $_POST['about_me'];
        $data['tagline'] = $_POST['tagline'];
    
        $this->db->set('personality', $data);
     }


What am I missing? Ive been reading the user quide and trying to find something on the forum but I cant seem to find anything.

Thanks for the help
#2

[eluser]kgill[/eluser]
Couple of quick thoughts since you didn't post any error messages, have you loaded the database class? CI's default configuration doesn't load it automatically. Second point, you're setting the data but you aren't actually calling the insert() method in active record. Go back and take another look at the user guide's active record stuff - there's enough examples there to get you where you want to go. And just a comment, dumping the post vars straight into the DB like that is generally not the best idea if you're concerned about security.

- K
#3

[eluser]Christopher Blankenship[/eluser]
Ok. putting my two cents into the mix.

Here is what I like to do to avoid all those $_POST statements.
Code:
/* controller: add_task */
   $this->load->helper('form');
   $data = array('title' => "Add Task");
   $data['pid'] = $this->uri->segment(3);
   $this->load->view('add_task', $data);

/* view: add_task  */
   echo form_open('welcome/add_task_rec');
   echo form_hidden('rec[pid]', "$pid");
   echo form_textarea('rec[notes]', '');
   echo form_submit('Submit', 'Submit');

/* controller: add_task_rec
* this however could be separated out to the model
* if so wished but the point is to pass all the information
* as an array to avoid the $_POST groups.
*/
   $this->db->insert('ticket', $_POST['rec']);
   redirect("$base_url");
That should at least shorten your coding efforts and help with cleaner code. The code is just an example and I assume you have already gone to application/config/autoload.php and set the following:
Code:
$autoload['libraries'] = array('database');
#4

[eluser]Bulk[/eluser]
Take another look - you're not using the database functions properly - you seem to be cofusing the set function with insert.

Also as previously mentioned dumping POST variables direct in to a database is very bad form - you should look at the input class functions

// Edit: didn't read the rest of the thread properly, kgill said most of what I said
#5

[eluser]Christopher Blankenship[/eluser]
I agree with Bulk, passing post variables directly into a database is bad form; if you intend the application to be for anything other than a quick and dirty example (which I apologize for) or in this case a task management system for myself which is not accessible by anyone but myself. So to that end:
Code:
/* controller: add_task_rec
* this however could be separated out to the model
* if so wished but the point is to pass all the information
* as an array to avoid the $_POST groups.
*/
   $this->db->insert('ticket', $_POST['rec']);
   redirect("$base_url");
would be as follows:
Code:
/* controller: add_task_rec
* this however could be separated out to the model
* if so wished but the point is to pass all the information
* as an array to avoid the $_POST groups.
*/
   $recs = $_POST['rec'];
   $this->db->insert('ticket', $recs);
   redirect("$base_url");




Theme © iAndrew 2016 - Forum software by © MyBB