How does html connect to database data?
Hello @Sign, Show It is simple to create contact form to send email or any other information and storing in mysql database. Using INSERT INTO statement you can insert new data into a database table. See the example below: Output: Step 1: Connection with Database This is dbConn.php file which is used to connect the database. This dbConn.php file will use everywhere, where you want to perform a task with the database. Here it is connected with the local database and check database connected or not. dbConn.php Step 2: Creating HTML form and inserting data Here's a simple HTML form which is index.php file. It has two text input and a submit button. The INSERT INTO statement is used to add new data into the database. This insert query will execute passing through to the PHP mysqli_query() function to insert new data. And also created a simple HTML form which has two input text and a submit button which is used to take data from users. In the index.php file the dbConn.php file includes for connecting with the database and insert new data. The form has two field fullname and age which are get data from users and store into the database. In this example the PHP code and the HTML form coded in the single page. The PHP script embedded with the HTML page. index.php
Write PHP code as a separate file Step 1: Creating HTML Form First of all creating HTML form which is index.php file. This form has two text boxes and a submit button for inserting data. This form also has an action attribute which contains insert.php. This will perform when clicking on submit button. index.php
Step 2: Connection with Database This is dbConn.php file which connects the database. dbConn.php Step 3: Write PHP code for insert data This is insert.php file. Here is written PHP code for inserting data when clicking on submit button. insert.php Hope it helps!! IntroductionThe following steps are required to design a registration form (Sign Up Form): Step 1: Firstly, install a virtual server on your computer (eg Xampp, Wamp). Xampp is a free and open-source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MySQL database, and interpreters for scripts written in the PHP and Perl programming languages. XAMPP stands for Cross-Platform (X), Apache (A), MySQL (M), PHP (P), and Perl (P). Figure: Xampp Server Step 2: Next we will require an editor where the HTML code has to be written. You can use any editor (such as Notepad++, Adobe Dreamweaver, NetBeans, etc). Here we will use Notepad ++. Step 3: Install Notepad++. Here are the steps: Figure: Notepad ++ Notepad++ is a source code editor used with Microsoft Windows. It supports editing in tabular form, which allows us to work with multiple open files in a single window. Notepad++ is distributed as free software. Click on the first link in the browser window. After clicking the first link the following window will appear. Figure: Download Notepad ++ Download the software by clicking the download button. Step 4: Open the Notepad++ text editor and write the HTML code for designing the HTML Sign Up page.\ We will use various HTML tags to design the page. You can include the fields according to your convenience (i.e whichever fields you require for the form). Here I have included the fields according to my convenience. Have a view of the code written in notepad++,
Here I have included the LINK tag to link the CSS file for this HTML page. HTML or Hypertext Markup Language is the standard and most basic language in use to create web pages. CSS stands for Cascading Style Sheets This is used for styling purpose. HTML coding is just a structure and CSS is applied to dictate your website's look and feel. Font size, font color, font style styling of images, page layout, mouse-over effects and more are determined by CSS. The CSS applied over the above HTML coding is given below.
Here you would be wondering why I have used The We need to see how to use them later on in the body section of the page. To use your class, all you need to do is add the class=" " attribute to the tag you wish to have the style of your class. So, if you wanted a line of text to be read,
you could add the class attribute to a Now let us move further. Step 5: Apply CSS on the HTML code. General rules for applying CSS are: We use dot(.) beside any class to apply effects into it and ‘#’ tag before any
ID. E.g. .container { // css attributes will be written here } Here I have taken container as a class. Class is user-defined. Note: Save the Sign_Up page in the xampp folder->htdocs->create a new folder( user-defined). Inside this new folder, you have to keep all the data related to your project. It may be any kind of images used in webpage, HTML
coding, or CSS coding. I mean to say all the things that are used in creating a web page must be under one roof (i.e under one folder). Figure: Sign_Up After writing the HTML code and applying the above CSS, the registration page would look like above. Next, we will insert data into the fields of the sign_up page and store the information in MySQL. For that, we have to start the xampp controller. Start Apache and MYSQL in the XAMPP controller. Now we will go to the next level where we will make use of PHP syntax. PHP
PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. PHP is an acronym for "PHP: Hypertext Preprocessor". Figure: Xammp Start PHP is a widely-used, open-source scripting language. PHP scripts are executed on the server.
PHP is free to download and use. PHP code is executed on the server, and the result is returned to the browser as plain HTML. PHP files have extension ".php". PHP can collect form data. PHP can add, delete, modify data in your database. It runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) and supports a wide range of databases. PHP is easy to learn and runs efficiently on the server-side. PHP Variables (Rules) Output Variable The PHP ‘echo’ statement is often used to output data to the screen. PHP FORMS
Write Html coding for sign_up page and save it as index.php. Again create a PHP page named connection.php where we will write the code for creating a connection with the database. The PHP script are always written between ( closing). Here as you are seeing that I have used user-defined variables to assign something. This makes me easy to write code effectively and in a clean way. Here I have assigned localhost to $servername, ’root’ to $username and password has been left blank. Again I have written mysql_connect()—this is mainly used to open a connection to MySQL server. Again we have used
mysql_select_db()—this is used to select the database created in localhost/phpmyadmin. Now let us create a database in MySQL. The PHPMyAdmin window will look like the following screenshot: Figure: phpmyadmin Here you can create your own database and as many tables inside that.
Suppose here I have created a database named ‘test’, inside that I have created a table named ‘registration’. In this table, we will create fields that are used in our registration form such as fname, mname, lname, pwd, cnf, mail, number, sex, address, code, city, country, skills, attach_file. Note: Here we can write any name for the concerned fields, just keep in mind the names you have given for the relative fields. You can create a table manually or by
writing queries. Here, I have created it manually. Figure: Insert fields Here write data type (most
of the time we use varchar), length in numbers. After filling the fields name click Go to continue. After inserting you can view the inserted fields where the data will be fetched from the form. The structure is as in the following screenshot: Figure: Structure Now create another php page and name it modified.php. On this page, we will
write code to see how we can fetch data from the fields. For this, we will make use of form methods through which we will retrieve the data entered into the fields and store it into our MySQL database. The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post"). Basically we use
any of the following two methods to fetch data from the fields. Here I have included listing.php file because in this PHP file I have written the code for listing all the inserted data. Now write a php code in this page only. Note: We have to include connection.php file in all the pages because the connection code has been written in connection.php file only. Until and unless the connection is established, the data can’t be sent to the database. Here I have written an IF-ELSE condition to check whether any id is coming or not. The isset () function is used to check whether a variable is set or not. Then I have written a query for delete and saved in a local variable named $sql. After that mysql_query() is used to execute the query written above. Again we have written a query to select all the elements of the table created. On the same page that is modified.php, now we will write another group of code for displaying the data into the table created. For that, we will write a while condition where we will fetch the data from the database until and unless the data is coming. Here I have saved the query mysql_fetch_object() into a local variable ‘$row’. Then I have echoed the relative fields data into the corresponding table entry. Now this tutorial of insertion of data into the database and showing it in the form of lists has been successfully completed. Have a look at the
final result. Figure: Final View |