Paste into database using codeigniter

Hi

I want to insert data using a form in CI, but I ran into a 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 the help

+3
source share
3 answers

Your form is being sent to welcomedb/submit, but your controller is in welcomedb/simpleform... maybe you need to change this.

Otherwise, something seems to be wrong.

+10
source

Maybe:

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

Remove the comma from the last item (age) as follows:

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

and always reset the error.

+2
source
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>
+1

Source: https://habr.com/ru/post/1751402/


All Articles