EllisLab text mark
Advanced Search
     
Model/Controller Quick Question
Posted: 02 July 2007 07:21 PM   [ Ignore ]
Joined: 2007-06-07
4 posts

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.

function insert()
{        
$this
->load->model('Test');
$this->Test->insert_info();

and then the model has

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

 
Posted: 02 July 2007 07:49 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2006-11-08
231 posts

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

 
Posted: 02 July 2007 08:28 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-13
70 posts

Ok. putting my two cents into the mix.

Here is what I like to do to avoid all those $_POST statements.

/* 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:

$autoload['libraries'= array('database'); 
 
Posted: 03 July 2007 05:18 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2002-08-31
23 posts

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

 
Posted: 03 July 2007 02:29 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-13
70 posts

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:

/* 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:

/* 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");