Bob Bobby Bob Bobby - 2 months ago 12
MySQL Question

How to combine PHP and HTML files to access/manipulate database


I am trying to create a web app that will access to my database, and be able to SELECT, INSERT, UPDATE and DELETE records. I am new to using databases and PHP, and have looked at other questions trying to make a breakthrough in my understanding, so please be kind and don't mark this as a duplicate.


I have an html file containing my webpage that is a scheduling tool, where users can plan meetings with others. I have created a database and understand the PHP code to manipulate that database, but I don't know where to do this.


Say, for example, I have a PHP function that takes in a meeting time, and meeting description, and logs it into the database (not exactly what I'm trying to do but will be good enough for me to understand). I want it so that when a user clicks a segment on my page and logs in that meeting info, for it to call that PHP function and log in the data. Currently, I have one html file and 3 js files containing all the information for my page. I see that html information can be put into a php file, but not vice versa. So, do I:

A.) Create a separate php file with all of the functions I will need to manipulate my database, and call those functions from my html and javascript files?

B.) Create my entire page in a php file, rather than an html file, so I can intersperse snippets of php to manipulate my database?

C.) I'm not understanding it correctly and there is something else I should do?

I understand this is a very basic question but I haven't found an answer that has enlightened me thus far, and I think this could be a useful question for beginners in the future.


The general answer to your question is MVC framework. If you want to build web app, it will make your life easier in many ways: routing, communication with database, managing data, splitting frontend from backend. There are a lot of them out on the internet, the one I can recommend is Laravel. Although you can use any with which documentation will you be satisfied. Symfony, Zend, CodeIgniter and much more.

From your question it looks like you lack information about PHP and general app building mechanisms, so trying to make your own base for app from scratch may result in disaster and discouraging you to continue. If you have some free time, the easiest way to gain knowledge should be books. They will guide you from the very first letter in code till the end. Just be careful to buy a good book, so you won't spend time learning bad habits (just read book's comments).

Although, I will try to briefly answer the questions you stated. It shouldn't be hard to find out more on the internet.

The most common way to communicate from frontend (html) to backend (php) is by forms. You create a form with action targeted on PHP file script (it may be even the same page, but in general we avoid that). You can use for example POST method. Thanks to it, after user fills it and clicks submit button, PHP script will receive all data via POST. In script you can use $_POST, which is a global collection of data sent with a form. Let's look at the example:

<form action="this.php" method="POST">
  <input type="text" name="name" placeholder="Please enter your name">
  <input type="email" name="email" placeholder="Please enter your email">
  <input type="submit">

in this.php file there will be available: $_POST['name'] and $_POST['email']. You can now work on them and finally put them into database.

Read more:

There is also GET methods. It allows you to send data via url parameters. Let's say you have too much data in database, and you need to split it into pages. You can create a href "mytestpage.php?page=2" that will represent what page you are currently on. You can read this attribute with $_GET['page'].

Read more:

If you use plain PHP, always be sure to take precautions. Have in mind that user can pass dangerous data and violate your scripts.

Other also common way to communicate user with backend is AJAX. It uses javascript to asynchronously communicate with scripts and run them. For example you want to create newsletter box that doesn't refresh site and stop user from reading rest of your article. It may not be the finest way to use AJAX (believe me it is far more powerful), but it is the situation you would consider it. It works like this: user submits form - that data is intercepted by javascript, which make asynchronous call to server to pass the data - user doesn't see this nor it takes any effect on his browsing. AJAX is pretty easy to use with jQuery javascript library.

Read more: