MySQL insert with HTML Form using CodeIgnitor

Wednesday, October 28, 2015
Using Core PHP, you can submit form and insert row in MySQL table using a single file. However, the method can be insecure. Using MVC model of CodeIgnitor, you can create a secure method. I can think of simple 1 php page form in an instance but my head hurts when implementing it on MVC model. So, here is tutorial for everyone including myself. The tutorial begins with basic step considering you have already installed WAMP or LAMP and unzipped codeignitor.
  1. Configure connection to database in application/config/database.php.
    $db['default'] = array(
     'dsn' => '',
     'hostname' => 'localhost',
     'username' => 'root',
     'password' => 'password',
     'database' => 'databasename',
    
  2. Add database library to application/config/autoload.php
    $autoload['libraries'] = array('database');
    
  3. Create a model to insert data in MySQL in /application/models/Insert_model.php
    <?php
    class Insert_model extends CI_Model{
    
     function form_insert($data)
     {  
      $this->db->insert('groups', $data);
     }
    }
    ?>
    
  4. In MySQL, my table consists of 2 columns: group and description. below is the controller /application/controller/Form.php
    <?php
    class Form extends CI_Controller
    {
     function index()
     {
      $this->load->library('form_validation');
      $this->load->view('form_view.php');
     }
    
     function insert()
     {
      $this->load->library('form_validation');  
      $this->load->model('Insert_model');
      
      $this->form_validation->set_rules('group', 'Group', 'required');  
      $this->form_validation->set_rules('desc', 'Description');
    
      if ($this->form_validation->run() == FALSE) 
      {  
       $this->load->view('form_view.php');
      } 
      else 
      {     
       $data = array(
       'group' => $this->input->post('group'),
       'desc' => $this->input->post('desc')   
       );
       
       $this->Insert_model->form_insert($data);
       
       $data['message'] = 'Data Inserted Successfully';   
       
       $this->load->view('form_success_view.php', $data);
      }
     }
    }
    ?>
    
  5. Create view file for form as application/views/form_view.php
    <!DOCTYPE html>
    <html>
     <head>
     </head>
     <body>
      <h1>Add Group</h1>
      <?php echo validation_errors(); ?>
      <?php echo form_open('form/insert'); ?>  
       <label>Group Name: <label><input type="text" name="group" /><br/>
       <label>Description: <label><input type="text" name="desc" /><br/>
       <input type="submit" name="submit" value="Submit" />  
      </form>
     </body>
    </html>
    
  6. Finally, Create a success page as application/views/form_success_view.php
    <!DOCTYPE html>
    <html>
     <head>
     </head>
     <body>
      <h1><?php echo $message ?></h1>
     </body>
    </html>
    
  7. Test your form
    http://localhost/index.php/form
    

Thats all! If you face any error please comment below.

No comments: