Welcome Guest, Not a member yet? Register   Sign In
Inserting form data into a database
#1

[eluser]mdcode[/eluser]
Hopefully this is the last one from me for a while, but I am having trouble inserting data from a form into a database. When the form is loaded, data filled in and then submitted, the page refreshes with a reset (blank) form and no data has been inserted into the db, even though there are no error messages.

I have checked all field names and corresponding column names in the 'projects' table and confirm them to be correct. I have also gone through several articles here in the forums and on other sites, and through two tutorials out there but still no joy.

CONTROLLER:
Code:
if($this->input->post('submit_form'))
{
    $this->projects_model->insert();
}

MODEL:
Code:
/* inserting the project data */
function insert()
{
$data = array(
    'customer'=>$this->input->post('customer', TRUE),
    'division'=>$this->input->post('division', TRUE),
    //'date_raised'=>'CURDATE()',
    'date_required'=>$this->input->post('date_required', TRUE),
    'tech'=>$this->input->post('tech', TRUE),
    'rep'=>$this->input->post('rep', TRUE),
    'priority'=>$this->input->post('priority', TRUE),
    //'job_status'=>'Not Started',
    'allocated_time'=>$this->input->post('allocated_time', TRUE),
    );
            
    $this->db->insert('projects',$data);
}

And the VIEW:
Code:
<?php echo form_open('projects/add'); ?>

<?php echo form_fieldset('Main Details'); ?>
    <table class="table100">
        <tr>
            <td width="10%">Customer</td>
            <td width="40%">&lt;?php echo form_dropdown('customer', $customers, '', $dds); ?&gt;</td>
            <td width="10%">Division</td>
            <td width="40%">&lt;?php echo form_dropdown('division', $divisions, '', $dds); ?&gt;</td>
        </tr>
        <tr>
            <td width="10%">Date Required</td>
            <td width="40%">&lt;?php echo form_input($date_required); ?&gt;</td>
            <td width="10%">&nbsp;</td>
            <td width="40%">&nbsp;</td>
        </tr>
        <tr>
            <td width="10%">Technician</td>
            <td width="40%">&lt;?php echo form_dropdown('tech', $techs, '', $dds); ?&gt;</td>
            <td width="10%">Representative</td>
            <td width="40%">&lt;?php echo form_dropdown('rep', $reps, '', $dds); ?&gt;</td>
        </tr>
        <tr>
            <td width="10%">Priority</td>
            <td width="40%">&lt;?php echo form_dropdown('priority', $priority, 'Medium'); ?&gt;</td>
            <td width="10%">Allocated Time</td>
            <td width="40%" style="font-weight:normal;">&lt;?php echo form_input($allocated_time); ?&gt; hours</td>
        </tr>
    </table>
&lt;?php echo form_fieldset_close(); ?&gt;
&lt;?php echo form_submit($submit_form, 'Add Project'); ?&gt;

Any and all help is gratefully received.
#2

[eluser]eoinmcg[/eluser]
Hi,

Possibly the $submit_form var (last line in your view) isn't set to 'submit form'?
Code:
&lt;?php echo form_submit('$submit_form', 'Add Project'); ?&gt;

Try changing the last line in the view to
Code:
&lt;?php echo form_submit('submit_form', 'Add Project'); ?&gt;

Hope this helps!
#3

[eluser]mdcode[/eluser]
Damn those $ signs... thanks mate, also looking over the documentation again I saw that you were also right on the quotes situation... things now insert.
#4

[eluser]eoinmcg[/eluser]
Glad to be of help


Also, adding this to the controller's constructor helps keep track of inputted data, sql executed etc.
Code:
$this->output->enable_profiler(TRUE);

Very handy when debugging!
#5

[eluser]kgill[/eluser]
Just a quick tip, the $this->input->post stuff in your model, move it to your controller. The less your model knows about where the data is coming from the better. If your function in the model takes parameters instead, then in the future if you need to add a different way to collect your data (GET, built via code, whatever) it won't matter because your function is only concerned with did you pass me the right params not did you submit a form via post.




Theme © iAndrew 2016 - Forum software by © MyBB