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 

40 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
  8. this post is very imformative for developer who wants more information regarding the programming language.thanks for sharing this post.
    php training in nagpur

    ReplyDelete
  9. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    iOS App Development Company
    iOS App Development Company

    ReplyDelete
  10. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    iOS App Development Company
    iOS App Development Company

    ReplyDelete
  11. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text
    Mobile Marketing Services
    mobile marketing companies
    Sms API

    ReplyDelete
  12. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text
    Mobile Marketing Services
    mobile marketing companies
    Sms API

    ReplyDelete
  13. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  14. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    ReplyDelete
  15. Awesome data for me....thanks for sharing the instructive post...am took in some data about PHP. PHP Training Institute in Ahmedabad , PHP classes in Ahmedabad

    ReplyDelete
  16. Very good PHP tutorial for learning php. Thanks.
    Software Company in Dehradun

    ReplyDelete
  17. I simply want to give you a huge thumbs up for the great info you have got here on this post.
    Oracle training in chennai

    ReplyDelete
  18. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Devops Training in Chennai

    Devops Training in Bangalore

    AWS Training in chennai

    AWS Training in bangalore


    ReplyDelete
  19. Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!
    python training in tambaram
    python training in chennai
    python training in annanagar
    python training in chennai

    ReplyDelete
  20. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.

    Blueprism training in tambaram

    Blueprism training in annanagar

    Blueprism training in velachery

    ReplyDelete
  21. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 
    Data Science course in kalyan nagar | Data Science course in OMR
    Data Science course in chennai | Data science course in velachery
    Data science online course | Data science course in jaya nagar

    ReplyDelete
  22. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
    java training in chennai

    java training in marathahalli | java training in btm layout

    ReplyDelete
  23. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here 
    angularjs Training in chennai
    angularjs-Training in pune

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    ReplyDelete
  24. I’m planning to start my blog soon, but I’m a little lost on everything. Would you suggest starting with a free platform like Word Press or go for a paid option? There are so many choices out there that I’m completely confused. Any suggestions? Thanks a lot.



    AWS Training in NewYork City | Amazon Web Services Training in Newyork City


    AWS Training in London | Amazon Web Services Training in London, UK

    Amazon Web Services Online Training in USA | AWS Online Course in USA

    ReplyDelete
  25. Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing.


    AWS Tutorial |Learn Amazon Web Services Tutorials |AWS Tutorial For Beginners

    AWS Training in Chennai |Best Amazon Web Services Training in Chennai


    Amazon Web Services Training in Pune | Best AWS Training in Pune

    ReplyDelete
  26. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  27. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  28. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    Data Science training in rajaji nagar | Data Science Training in Bangalore
    Data Science with Python training in chennai
    Data Science training in electronic city
    Data Science training in USA
    Data science training in pune

    ReplyDelete
  29. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    python course in pune
    python course in chennai
    python course in Bangalore

    ReplyDelete
  30. Your story is truly inspirational and I have learned a lot from your blog. Much appreciated.

    Best Devops Training in pune
    excel advanced excel training in bangalore

    ReplyDelete
  31. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    AWS Training in Bangalore |Best AWS Training Institute in Bangalore BTM, Marathahalli
    AWS Training in Chennai | AWS Training Institute in Chennai Velachery, Tambaram, OMR

    ReplyDelete
  32. Thank you for an additional great post. Exactly where else could anybody get that kind of facts in this kind of a ideal way of writing? I have a presentation next week, and I’m around the appear for this kind of data.
    python course in pune
    python course in chennai
    python course in Bangalore

    ReplyDelete
  33. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete
  34. This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete