PHP MVC CodeIgniter Laravel Core PHP MORE

How to insert data in database using PHP MVC with example


In this example we using Models, Views, Controller Structure for insert data into the database.

To insert data in the database first we have to create a Controller file.

controller/index.php

<?php
class Index extends Controller {
    function __construct() {
		parent::__construct();
	}
	
	function index() {
	
		$this->view->allrecords = $this->model->getAllrecords();
		$this->view->render('index/index');
		
	}
	function edit_submit_index(){
		
		$action=$_POST['submit']; 
		if ($action=='submit')
		{
			echo'$action';
		$arg=$_POST['id'];
		$data = array(
		'id' =>null,
		'name' =>$_POST['name'],
		'email' =>$_POST['email'],
		'contact' => $_POST['contact']
		);
		$this->model->submit_index($data);
		
		}
		header('location: index');
		}
		}
		

Here is the model file which we are using for inserting data to database.

The file index_model.php is created under Models folder

models/index_model.php

index_model.php

<?php

class Index_Model extends Model
{
	public function __construct()
	{
		parent::__construct();
	}
	public function getAllrecords()
	{
		return $this->db->select("SELECT * FROM `mvc` ORDER BY id DESC");
	}
	public function submit_index($data)
	{
		$this->db->insert('mvc', $data);
	}
	}
	

Here is the view file index.php which inside views folder contains the form

views/index/index.php

<?php
  $name=$this->content[0]['name'];
  $email=$this->content[0]['email'];
  $contact=$this->content[0]['contact'];
?>

<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
                           
  <form action="<?php echo URL; ?>index/edit_submit_index" method="post" enctype="multipart/form-data" onsubmit="return confirm('Do you really want to submit the form?');">

    <div class="col-xs-6 form-group">
      <label class="control-label col-xs-6" for="name">name:</label>
      <div class="col-xs-6">
        <input class="form-control" name="name" id="name" placeholder="Enter name">
      </div>
    </div>
	<div class="col-xs-6 form-group">
      <label class="control-label col-xs-6" for="email">email:</label>
      <div class="col-xs-6">
        <input type="text" class="form-control" name="email" id="email" placeholder="Enter email">
      </div>
    </div>
    <div class="col-xs-6 form-group">
      <label class="control-label col-xs-6" for="contact">contact:</label>
      <div class="col-xs-6">         
        <input type="text" class="form-control" name="contact" id="contact" placeholder="Enter contact">
      </div>
    </div>

    <div class="col-xs-6 form-group">        
      <div class="col-sm-offset-2 col-xs-6">

       
      <button type="submit" class=".btn-info form-control" value="submit" name="submit">Submit</button>
	 

      </div>
     </div>
    </form>
   </div>
  </div>
 </div>
Path: localhost/project_folder_name/view_folder_name/view_filename
Example: localhost/mvc/index/index