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 

44 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. 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
  4. 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
  5. 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
  6. Very good PHP tutorial for learning php. Thanks.
    Software Company in Dehradun

    ReplyDelete
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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

  16. 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

  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. This comment has been removed by the author.

    ReplyDelete
  23.   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
  24. 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
  25. 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
  26. 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
  27. 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
  28. This looks absolutely perfect. All these tiny details are made with lot of background knowledge. I like it a lot. 
    Tableau Training in Chennai

    Tableau Training in Bangalore

    Tableau Training in Bangalore

    ReplyDelete
  29. Nice Post! Thank you for sharing knowledge, it was very good post to update my knowledge and improve my skills. keep blogging.
    Java Training in Electronic City

    ReplyDelete
  30. I Got Job in my dream company with decent 12 Lacks Per Annum Salary, I have learned this world most demanding course out there in the current IT Market from the Data Science Training in Pune Providers who helped me a lot to achieve my dreams comes true. Really worth trying.

    ReplyDelete
  31. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.. it was so good to read and useful to improve my knowledge as updated one, keep sharing more information…!!Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

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

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

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

    ReplyDelete
  35. I'm dazzled, I should say. Only sometimes do I go over a blog that is both educative and intriguing, and doubtlessly, you have nailed it. The issue is an issue that insufficient people are talking cleverly about. Presently i'm upbeat I unearthed this during my chase for something with respect to this. news updates

    ReplyDelete