How To Create Database In Html Code
Web development basics -How to connect HTML Form to MySQL using PHP on Apache web-server Part-1.
A step by step guide to create a very basic dynamic web page. I am using Ubuntu 16.04 machine.
This article in a nutshell:
- Download prerequisites
2. Start the web server
3. Create a new MySQL database with table and columns
4. Create an HTML form to collect data.
5. Create a PHP file to connect to the database that will accept the submitted form data and add it to the database.
6. Run the HTML form file on the web server, add user data and check if the database is populated.
First-up is the environment setup:
Code editor (A developer's code editor is a personal choice. You can use any of your favourite. I have Visual Studio Code editor. It can be downloaded from here https://code.visualstudio.com/download)
Installing Apache 2 web-server on Ubuntu local machine via the terminal.
$ sudo apt-get update
$ sudo apt-get install apache2
Once installed, open your web browser and type localhost. It should open Apache web server information page. This means your web server is running now. Some additional commands to start, stop and restart the web server which will be needed later are:
$ sudo [add space]/etc/init.d/apache2 [add space] start
$ sudo [add space]/etc/init.d/apache2 [add space] stop
$ sudo [add space]/etc/init.d/apache2 [add space] restart
By default Apache2 web server runs all the files from /var/www/html folder located in computer .
Next we will create a new HTML and PHP file in this /var/www/html folder.
Installing MySQL with Apache web server
$ sudo apt get install mysql-server
Installing PHP with Apache web server
$ sudo apt install php libapache2-mod-php
Once PHP is installed, restart Apache2 web server using the restart command and create a test.php file. Open this file in the code editor and write the code.
$ sudo touch [space here] /var/www/html/test.php
Run this file in your browser's localhost. It should display 'Hello World' indicating that PHP is working fine.
MySQL database
$ sudo -u root -p
mysql> show databases;
mysql> CREATE DATABASE home;
mysql> CREATE TABLE people ( name VARCHAR(20), email VARCHAR(20));
I forgot to add an ID so I used ALTER TABLE command
mysql>ALTER TABLE people ADD ID int NOT NULL;
mysql>ALTER TABLE people MODIFY ID int NOT NULL AUTO_INCREMENT;
To view what is created
mysql> desc info;
So the table is ready now its time to code a simple HTML form in the folder with path var/www/html/basicform.html . Open this file in the code editor and write the code.
HTML basic form
$ sudo touch [space here] /var/www/html/basicform.html
Run this file in your browser's localhost. It should display the form.
Now its time to create a PHP file and add some PHP code. This code will enable the database to be populated by form data.
PHP Code
$ sudo touch [space here] /var/www/html/basicphp.php
Run the basicform.html file in the localhost, add the user data and hit submit.
Finally, check the database.
Check Populated Database
mysql> SELECT * FROM people;
Hope this has helped you.Please do let me know in the comments below. Thank you and Happy Coding.
How To Create Database In Html Code
Source: https://medium.com/analytics-vidhya/web-development-basics-how-to-connect-html-form-to-mysql-using-php-on-apache-web-server-part-1-7edce564169e
Posted by: berryhalseara.blogspot.com
0 Response to "How To Create Database In Html Code"
Post a Comment