infolink

Monday, 5 October 2015

Insert Into MySQL PHP MVC

This tutorial helps to developer when developer want to insert data into database (MySQL) with PHP MVC. MVC contains on an Model, View and Controller. In a Model programmer write the code related to Database or business logic. In View create the layout or forms  and using the Controller Model return the data on a View.
To insert into mysql php mvc we follow the few simple steps.

Step 1:

Create the 3 folders with the name Model,View and Controller respectively. Also Create a index.php file.
Folders and file which is used

In Controller folder create Controller.php.
In Model folder create Model.php.
In View folder create view.php.


Step 2:

in the above mention index.php file write the following code:

<?php
include_once("Controller/Controller.php");
$Controller = new Controller();
$Controller->show();
?>

Step 3:

Now in the Controller folder create a file with name "Controller.php" and write the  bellow mentioned code.

<?php
include_once("Model/Model.php");

Class Controller{
public $model;

public function Controller()
{
$this->model=new Model();
}

public function show()
{
$result = $this->model->insert_data();
include 'View/view.php';
}
}
?>

Step 4:

Create a "Model.php" file inside the Model folder and write the code like this.

<?php

Class Model{

public function Model()
{
$con = mysql_connect("localhost","root","") or die('Not connected to Database<br>' . mysql_error());
mysql_select_db("mvc_db",$con) or die('Not Select Database' . mysql_error());
}

public function insert_data()
{
if (isset($_POST['name']) && isset($_POST['age']) && isset($_POST['name'])) {
$name = $_POST['name'];
$age = $_POST['age'];
$city = $_POST['city'];
$qry = "insert into table1 (name,age,city) values('$name',$age,'$city')";
$result = mysql_query($qry);
if ($result) {
return "Insert Data Successfully.";
}
else
{
return "Error...! Not Inserted.";
}
}
else
{
return "";
}
}
}
?>

Step 5:

At last create the "view.php" file in View folder and write the following code. It's simple html base form.

<!DOCTYPE html>
<html>
<head>
  <title>Insert Data in MySQL using PHP MVC</title>
</head>

<body>
<?php 
 echo $result;

?>
<form action="" method="POST">
  <p>
   <label>Name</label>
   <input id="name" value="" name="name" type="text" required="required" /><br />
  </p>

  <p>
   <label>Age</label>
   <input id="age" name="age" type="text" required="required" /><br />
  </p>

   <p>
   <label>City</label>
   <input id="city" value="" name="city" type="text" required="required" /><br>
  </p>
   <br />
  <p>

      <button type="submit" class="green big" name="submit"><span>Save</span></button> <button type="reset" class="grey big"><span>Cancle</span></button>
  </p>
 </form>

</body>
</html>

output:

After complete all these five steps open the "index.php" file on browser. Form will display like this:
Insertion Form
Insertion Form
 Now enter the required field data and press Save button. If the record inserted then the "Insert Data Successfully" message will display as shown in bellow mention screen shot. and if the data will not inserted then  the "Error...! Not Inserted." message will display.

After data insertedsuccessfully
After data inserted successfully 

7 comments:

  1. plz give the discription with detail then it will be suberb

    ReplyDelete
  2. Step by step explanation is nice to understand.But i have learned html coding but php is something different so i need a detailed informative to learn more.


    Php Training in Chennai

    ReplyDelete
  3. Thanks for providing this informative information. it is very useful you may also refer- http://www.s4techno.com/blog/2016/08/17/ajax-and-php/

    ReplyDelete
  4. Hi to every one, the contents present at this site are
    actually remarkable for people experience, well, keep up the good work fellows.
    php training in chennai

    ReplyDelete
  5. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    iOS Training in Chennai
    Android Training in Chennai
    php Training in Chennai

    ReplyDelete
  6. Great post! I am see the great contents and step by step read really nice information.I am gather this concepts and more information. It's helpful for me my friend. Also great blog here with all of the valuable information you have.
    PHP Training in Chennai

    ReplyDelete

  7. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Android App Development Company

    ReplyDelete