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 

69 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
  35. In the beginning, I would like to thank you much about this great post. Its very useful and helpful for anyone looking for tips. I like your writing style and I hope you will keep doing this good working.
    aws training in bangalore
    AWS Training
    Aws Certification in Chennai
    best aws training in bangalore
    Best AWS Training in Chennai
    AWS Training in Chennai

    ReplyDelete
  36. I have gone through your blog, it was very much useful for me and because of your blog, and also I gained many unknown information, the way you have clearly explained is really fantastic. Kindly post more like this, Thank You.
    Air hostess training in Chennai
    Air Hostess Training Institute in chennai
    air hostess academy in chennai
    air hostess course in chennai

    ReplyDelete

  37. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa course in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete

  38. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa course in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  39. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    AWS Interview Questions And Answers | AWS Interviews Questions and Answers for Devops | AWS interview questions and answers for Sysops
    AWS Interview questions and answers for freshers | AWS Interview Question for devops
    AWS Training in Bangalore with placements | AWS Training in Bangalore cost

    ReplyDelete
  40. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    AWS Interview Questions And Answers | AWS Interviews Questions and Answers for Devops | AWS interview questions and answers for Sysops
    AWS Interview questions and answers for freshers | AWS Interview Question for devops
    AWS Training in Bangalore with placements | AWS Training in Bangalore cost

    ReplyDelete
  41. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

    Advanced AWS Training in Marathahalli |No.1 AWS Training in Marathahalli
    Best AWS Amazon Web Services Training Institute in Chennai | No.1 AWS Training Institutes for Solution Architect in Chennai | Advanced AWS Certification Training in Chennai

    ReplyDelete
  42. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.

    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune

    ReplyDelete
  43. Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage
    contribution from other ones on this subject while our own child is truly discovering a great deal.
    Have fun with the remaining portion of the year.

    Selenium training in bangalore | best selenium training in bangalore | advanced selenium training in bangalore | no.1 selenium training in bangalore

    ReplyDelete
  44. Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post
    rpa training in bangalore
    rpa training in pune
    rpa online training
    best rpa training in bangalore

    ReplyDelete
  45. It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.

    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training
    Selenium training in bangalore


    ReplyDelete
  46. This comment has been removed by the author.

    ReplyDelete
  47. 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 BTM LAYOUT | AWS TRAINING IN BANGALORE
    AWS Training in Marathahalli | AWS Training in Bangalore

    ReplyDelete
  48. 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 certification Training Online Bangalore
    AWS Certification Training Online Pune
    AWS certification Training Online Chennai
    AWS Online Certification and Training

    ReplyDelete
  49.   Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
    python Online training in chennai
    python Online training in bangalore
    python interview question and answers

    ReplyDelete
  50. I was recommended this web site by means of my cousin. I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!
    Data Science Training in Indira nagar
    Data Science Training in btm layout
    Data Science Training in Kalyan nagar
    Data Science training in Indira nagar
    Data science training in bangalore

    ReplyDelete
  51. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    AWS Training in pune
    AWS Online Training
    AWS Training in Bangalore

    ReplyDelete
  52. 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.
    microsoft azure training in bangalore
    rpa training in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  53. Thanks For Sharing your Information The Information Shared Is Very Valuable Please Keep Updating Us Time Just Went On reading the article Data Science Online Training Hadoop Science Online Training AWS Online Training Python Online Training

    ReplyDelete
  54. I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!

    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training


    ReplyDelete
  55. Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete