Welcome Guest, Not a member yet? Register   Sign In
[Problem] Inserting data from form_input to database
#1

[eluser]MarkTingson[/eluser]
Hi,

Can anyone help why can't I insert data from the form_input to my database. Here's the codes:

Controller:
Code:
$this->load->model('books_model');
            
if($this->input->post('submit')){
    $this->books_model->entry_insert();
}
Model
Code:
$data['year']        = 'Year';
$data['years']        = array('2007'=>'2007',
                '2008'=>'2008',
                    '2009'=>'2009');
$data['available']    = 'Available';
$data['summary']    = 'Summary';
$data['forminput']    = 'Form Input';

$data['ftitle']     = array('name'=>'title','size'=>30);
$data['fauthor']    = array('name'=>'author','size'=>30);
$data['fpublisher']    = array('name'=>'publisher','size'=>30);
$data['favailable']    = array('name'=>'available','value'=>'yes','checked'=>TRUE);
$data['fsummary']    = array('name'=>'summary','rows'=>5,'cols'=>30);
The entry_insert() function
Code:
function entry_insert(){
    $this->load->database();
    
    $data = array('strTitle'=>$this->input->post('title'),
        'strAuthor'=>$this->input->post('author'),
        'strPublisher'=>$this->input->post('publisher'),        
        'intYear'=>$this->input->post('year'),
        'strAvailable'=>$this->input->post('available'),
        'summary'=>$this->input->post('summary')
        );
                    
    $this->db->insert('books',$data);
}
Input View
Code:
<?php echo heading($forminput, 3); ?>
    <?php form_open('books/input'); ?>
    
    <?php echo $title         .':'.
                form_input($ftitle).br(); ?>
    <?php echo $author        .':'.
                form_input($fauthor).br(); ?>
    <?php echo $publisher    .':'.
                form_input($fpublisher).br(); ?>
    <?php echo $year         .':'.
                form_dropdown('year', $years).br(); ?>
    <?php echo $available     .':'.
                form_checkbox($favailable).br(); ?>
    <?php echo $summary     .':'.
                form_textarea($fsummary).br(); ?>
                
    <?php echo form_submit('submit', 'Submit!'); ?>
    <?php form_close(); ?>

I'm sure that my entry_insert() function works properly because if I only use
Code:
$this->books_model->entry_insert();
, it adds data to my database.

I'm confused what's happening here:
Code:
if($this->input->post('submit')){
    $this->books_model->entry_insert();
}

I'm stuck here for almost a day!

Thanks in advance!
#2

[eluser]flaky[/eluser]
First thing you should do, is remove data fetching from the model, put it in the controller, build the array then send it to the model for insert. In any step you could
Code:
print_r($array);
to see arrays content
#3

[eluser]MarkTingson[/eluser]
Thanks for the prompt reply.

Anyways, I used

Code:
form_input($ftitle);

to control the parameters of the text area.
Code:
$data['ftitle'] = array('name'=>'title','size'=>30);

Is there something wrong with it? I am actually using a magnifying glass just to trace if there were typing errors or unterminated codes. T_T
#4

[eluser]MarkTingson[/eluser]
Thanks flaky,

I'll do what you suggest.
#5

[eluser]MarkTingson[/eluser]
Hi flaky,
I did what you suggest and I think the problems is this code:
Code:
if($this->input->post('submit')){
    $data_2 = array(
        'strTitle'=>$this->input->post('title'),
                'strAuthor'=>$this->input->post('author'),                    
        'strPublisher'=>$this->input->post('publisher'),        
        'intYear'=>$this->input->post('year'),
        'strAvailable'=>$this->input->post('available'),
        'summary'=>$this->input->post('summary')
        );
        $this->books_model->entry_insert($data_2);
    }else{
        print_r($data_2);
    }
is not triggering when I click the submit button.
How's that?
Is there some code that triggers "onClick" event of a button?

Thanks!
#6

[eluser]flaky[/eluser]
should've seen it faster, anyways

instead of
Code:
<?php form_open('books/input'); ?>
      .
      .
      .
      <?php form_close(): ?>
put "echo" in front of form_open and form_close
Code:
<?php echo form_open('books/input'); ?>
      .
      .
      .
      <?php echo form_close(): ?>
#7

[eluser]MarkTingson[/eluser]
OMG! Im such a loser!! Thanks flaky! Its just the echo that I'm missing...

:*




Theme © iAndrew 2016 - Forum software by © MyBB