3

HI

i want to insert data using form in CI but i am facing the problem Here is my model code

function add_form() {
    $this->load->database();
    $id = $this->input->post('id');
    $name = $this->input->post('name');
    $age = $this->input->post('age');       
    $data = array(
       'name' => $this->input->post('name'),
       'age' => $this->input->post('age'),
    );
    $this->db->insert('user',$data);
}

Here is my controller code

function simpleform() {
    $this->load->helper('form');
    $this->load->helper('html');
    $this->load->model('welcomedb_model');
    if( $this->input->post('submit') ) {
        $this->welcomedb_model->add_form();
    }
    $this->load->view('welcomedb_view');
}

and here is my view code

<?php echo form_open('welcomedb/submit'); ?>
   <? echo $name; ?>: 
   <? echo form_input('name'); ?>
   </br>
   <? echo $age; ?>: 
   <? echo form_input('age'); ?>
   </br>
   <?php echo form_submit('submit', 'Submit'); ?>
   <?php echo form_close(); ?>

Thanks for your help

5
  • View code :<?php echo form_open('welcomedb/submit'); ?> <? echo $name; ?>: <? echo form_input('name'); ?> </br> <? echo $age; ?>: <? echo form_input('age'); ?> </br> <?php echo form_submit('submit', 'Submit'); ?> <?php echo form_close(); ?> Commented Jun 23, 2010 at 14:02
  • 1
    Please re-edit your question and format the code accordingly. You were close, but missed on the first try. Also, your view code should be there, not in a comment. Commented Jun 23, 2010 at 14:24
  • So... what exactly is the problem? Commented Jun 24, 2010 at 0:18
  • problem is when i submit a form no data is inserted in to database only the Null values Commented Jun 24, 2010 at 6:53
  • please provide any tutorial or code to perform the same Commented Jun 24, 2010 at 6:59

3 Answers 3

10

Your form is submitting to welcomedb/submit, but your controller appears to be at welcomedb/simpleform... maybe you need to change that.

Otherwise, there doesn't appear to be anything wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

Finally i found the solution it should be <?php echo form_open('welcomedb/simpleform'); ?> Thanks for help
2

Probably:

$data = array(
    'name' => $this->input->post('name'),
    'age' => $this->input->post('age'),
);

Remove comma from the last element (age) like this:

$data = array(
    'name' => $this->input->post('name'),
    'age' => $this->input->post('age')
);

and always dump error.

1 Comment

It's PHP not JS!! It's usually considered good practice to add a comma to the last array item.
1
Best way is to do these code ......
First conifg the autoload and database.
into autoload:$autoload['libraries'] = array('database'); 
              $autoload['helper'] = array('url','form','file');
Into controller

<?php
    class CDemo  extends CI_Controller
    {

        function index()
        {
            $this->load->view('VDemo');
        }
        function save()
        {
            $this->load->model('MDemo');

             if($this->input->post('submit'))
            {
                $this->MDemo->process();                
            }
            redirect('CDemo'); 
        }
    }
?>

Into Model
<?php
    class MDemo extends CI_Model
    {   
        function process()
        {
            $Id = $this->input->post('Id');
            $Name = $this->input->post('Name');
            $data = array(
                   'Id'=>$Id,
                    'Name'=>$Name                    
                    );
                    $this->db->insert('test',$data);    
            }
        }
?>

Into View
<html>
<head>
<title>DEMO</title>
</head>
<body>
    <h1>Form Biodata</h1>
    <?php
        echo form_open('CDemo/save', array('name' => 'VDemo'));    
    ?>
        <table>
            <tr>
                <td>Id :</td>
                <td><input type="text" name="Id"></input></td>
            </tr>
            <tr>
                <td>Name :</td>
                <td><input type="text" name="Name"></input></td>
            </tr>    
            <tr>
                <td><input type="submit" name="submit" value="submit"></input></td>
            </tr>        
        </table>
    <?php
        echo form_close();
    ?>
    <textarea rows="" cols="">Hello</textarea>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.