Web Programming TutorialsLearn to submit HTML data to MySQL database using php

Learn to submit HTML data to MySQL database using php

In this session we will design an html form ,add the data into it and submit it to the database by connecting it to MySQL database using php .

  • To design the html form follow the steps given below :

 

    1. Install the Xampp server in your machine and if it already installed just go to xampp -> htdocs folder .

 

    1. Create a new folder say addemp in the htdocs folder.

 

    1. In this addemp folder create a new file say index.php and open it with Notepad++ or any other text editor.

1

    1. Now for designing a form write the given code in index.php file .

 

<!DOCTYPE html>
<html>
<head>
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>


<title>Add Employee</title>
</head>
<body>

<form method="post" action="">
<label>First Name</label>
<input type="text" name="first_name" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label>Department</label>
<input type="text" name="department" />
<br />
<label>Email</label>
<input type="text" name="email" />

<br />
<input type="submit" value="Add Employee">
</form>



</body>
</html>

 

    1. The screenshots for Notepad++ window are shown below :

v2

    1. Now go to xampp control pannel and start the Apache and MySQL modules .

 

    1. Now to run your program enter the following url in the browser http://localhost/addemp/index.php and you will have the following output :

v3

    1. Now to have a view of the inserted data just create a new file called as process.php in the same addemp folder .

v4

    1. Write the code in process.php file

 

<?php

echo $_POST['first_name'];
echo '<br />';
echo $_POST['email'];

 

    1. So you will have the following view of your Notepad++ window :

v5

    1. Now when you run the process.php file , the data that you had inserted in the html form will get displayed .

v6
Note: It will display only FirstName and Email(As we have mentioned only the first name and email in the code) .

    1. Now to show all the data fields present in the html form , replace the code of process.php file with the given code :

 

<?php

print_r($_POST);

 

    1. Here is the screen shot of Notepad++ window :

v7

    1. Now run the index.php file and insert data in to the html form :

v8

    1. After inserting the data click on the add button which will pass this data to the process.php file and when you run the process.php file you will get the following output :

v9

    1. In our example , we need to get connected with the MySQL database , so let’s modify the process.php file .

 

    1. Now write the given code in the process.php file.

 

<?php include 'database.php';>

<?php

// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];

//Execute the query

mysqli_query($connect"INSERT INTO employees1(first_name,last_name,department,email)
				VALUES('$first_name','$last_name','$department','$email')");

Learn HTML5 Programming For Beginners

Note:-Here we had included the database.php file at the beginning in the code to get connected with the database.
In the process.php file we have to add the insert query to add the record in our database.

    1. To create a database connection we have to create new file say database.php in the same addemp folder.

 

    1. You will have the following view :

v10

    1. Now open the database.php file in Notepad++ or any other text editor and write the given code :

 

<?php
$connect=mysqli_connect('localhost','root','123','mydatabase');

if(mysqli_connect_errno($connect))
{
		echo 'Failed to connect';
}

?>

 

    1. You will have the following view of your Notepad++ window :

v11

    1. After creating the database connection , run the index.php file insert the data into it , this data will get added into your database which is present in the phpmyadmin of the Xampp.

 

    1. So add the data and click the Add Employee button :

v12

    1. If you run the process.php file it will display the blank window which means your data has been inserted into the database .

v13

    1. Now check your created employee table in the database in the phpmyadmin pannel :

v14

    1. Now to display the content in the browser , we have to add code that will check the if added condition :

 

    1. So add the given code in the process.php file .

 

<?php include 'database.php'; ?>

<?php

// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];

//Execute the query


mysqli_query($connect,"INSERT INTO employees1 (first_name,last_name,department,email)
		        VALUES ('$first_name','$last_name','$department','$email')");
				
	if(mysqli_affected_rows($connect) > 0){
	echo "<p>Employee Added</p>";
	echo "<a href="index.html">Go Back</a>";
} else {
	echo "Employee NOT Added<br />";
	echo mysqli_error ($connect);
}

 

    1. ScreenShot of Notepad++ window is shown below :

v15

    1. After inserting the data in the index.php file click the AddEmployees button we will be navigated to the process.php file.

 

    1. So if the data is inserted into the the database you will have the following window otherwise it will display an error message:-

Ultimate Java Development and Certification Course
v16

    1. In the code section of the process.php file we have added the link from where we can go back to the index.php file to add the record of the employee in the database.

 

    1. When we click on the GoBack link it will link to the index.php file.

 

    1. Thus you will be navigated to the index.php file .

v17

  1. Thus we have learnt to insert the data into the MySQL database by designing an html form and connecting the database using php.

82 COMMENTS

  1. goodday my name is eban emmanuel am a student in ghana. ive been learning html5 and ive got little issues with creating a form when creating a form in html5 the form webpage must it be stored as .php or can i store it as a html and connect with a phpscript connecting to my xamp database ..really need this for ma project will be glad to hear back from you

    • From what I’ve read the only difference between the html and php extensions are the extension themselves in other words you can have a php file that contains onlt html and vise versa. The big reason to use a php file extension is so as a developer you know that file contains php code. I like masking the files that contain php so that when I call them the user is not alerted to one I’m on a lennox server and two this file my contain code which can be hacked if you are good with php.

  2. I typed process.php but when I run it it is not working, it show me that

    Notice: Undefined index: first_name in C:\xampp\htdocs\addemp\process.php on line 3

    is there something i lost?

  3. I make HTML web on my computer desktop after that i installed xampp and xampp -> htdocs folder i make login php so, my question is that do i need to make index.php on it or not if so how about html that i created on desktop.that index.html also i have to move on xampp -> htdocs folder

  4. Hello there, I do believe your site could be having browser compatibility
    problems. Whenever I take a look at your web site in Safari, it looks fine however, if opening in Internet Explorer, it’s got some overlapping issues.
    I merely wanted to provide you with a quick heads up!
    Besides that, fantastic site!

  5. These problem dedected in xampp control panel ? plz give me it’s solution. plz sir

    11:56:14 AM [mysql] MySQL Service detected with wrong path
    11:56:14 AM [mysql] Change XAMPP MySQL and Control Panel settings or
    11:56:14 AM [mysql] Uninstall/disable the other service manually first
    11:56:14 AM [mysql] Found Path: C:\xampp\mysql\bin\mysqld –defaults-file=C:\xampp\mysql\bin\my.ini mysql
    11:56:14 AM [mysql] Expected Path: c:\xampp\mysql\bin\mysqld.exe –defaults-file=c:\xampp\mysql\bin\my.ini mysql

  6. When I followed the series of steps you given above, I got an error message like this:
    Parse error: syntax error, unexpected ‘index’ (T_STRING), expecting ‘,’ or ‘;’ in C:xampphtdocsaddempprocess.php on line 19.
    Kindly get me the rectification for the above error.

    • I am getting the error below after putting a comment section

      Notice: Undefined index: first_name in C:\xampp\htdocs\addemp\process.php on line 6

      Notice: Undefined index: last_name in C:\xampp\htdocs\addemp\process.php on line 7

      Notice: Undefined index: department in C:\xampp\htdocs\addemp\process.php on line 8

      Notice: Undefined index: email in C:\xampp\htdocs\addemp\process.php on line 9

  7. I am a student of imfi ict academy uyo, please How can i partition my site to have many columns and rows without using table?

  8. Guys Help this is my process.php

    and this is my index.php

    label
    {
    display:inline-block;
    width:150px;
    margin-bottom:10px;
    }

    Add Applicant

    Add Applicant

    First Name:

    Last Name:

    Passport Number:

    Date of Birth:

    Place of Issue:

    Date of Expiration:

    Email:

    and my database.php

    but it does not connect or save to my data base. Am i doing something wrong. please help

  9. I have to make a website in html.
    In which if the staff name and password correct then he can add documents.
    So how to make database and how can I connect with it with HTML file.

  10. 1st
    in process.php in the first two printscreens you don’t close the php it supposed to be like that? if yes then i have error when i’m running this file:
    Notice: Undefined index: first_name in C:xampphtdocsaddempprocess.php on line 4
    Notice: Undefined index: email in C:xampphtdocsaddempprocess.php on line 6

    2nd
    in the modification of process.php
    “<?php
    print_r($_POST); "
    my output is this: "Array ( ) "

    3rd
    You don't mention to create a database; what records, what data type … these infos are missing from the first steps when you first tell to open phpmyadmin to see the results!

    1st
    in process.php in the first two printscreens you don't close the php it supposed to be like that? if yes then i have error when i'm running this file:
    Notice: Undefined index: first_name in C:xampphtdocsaddempprocess.php on line 4
    Notice: Undefined index: email in C:xampphtdocsaddempprocess.php on line 6

    2nd
    in the modification of process.php
    "<?php
    print_r($_POST); "
    my output is this: "Array ( ) "

    3rd
    You don't mention to create a database; what records, what data type … these infos are missing from the first steps when you first tell to open phpmyadmin to see the results!
    4th
    in last insertion of data after executin index and then going to process.php
    " Parse error: syntax error, unexpected 'index' (T_STRING), expecting ',' or ';' in C:xampphtdocsaddempprocess.php on line 19 "

  11. Please folks, I’m just taking programming as a career and I’m loving it despite discovering the tasking part of it. For this I’m beginning with web design and development, still a fresher in it I can say I am, but believing persistent practice makes perfect. Now, the issue I’m facing is, I added password to my MySQL from the console and I discovered my phpMyAdmin shows me error anytime I try to access it. Please, any idea to come out of this will be highly appreciated. WampServer is what I use for more certainty in proffering solution. Thank you

  12. guys help me to find a solution for this problem:
    i have an php document with html form
    and i have date formated textbox
    i want to upload that data into the database using php

  13. Parse error: syntax error, unexpected ‘>’ in C:\xampp\htdocs\addemp\process.php on line 1
    this error is displayed for process.php file . please give me the solution for this

  14. That is the proper weblog for anyone who wants to search out out about this topic. You realize so much its virtually exhausting to argue with you (not that I actually would want…HaHa). You undoubtedly put a brand new spin on a subject thats been written about for years. Great stuff, simply great!

  15. I have recently started a web site, the info you provide on this web site has helped me greatly. Thanks for all of your time & work.

  16. Thank you so very much! It took me hours to get it to work (mistakes here and there) but I think I learned a lot on the way. All the best to you.

  17. Hello Dear ,
    I have just visit your blog & found your blog’s post are very useful . I like Your every post because you have discussed what I am looking for . Thank you For your nice posting . I hope That You will be continuing this kind of posting .

  18. Hello there, You have done a great job. I’ll definitely digg it and personally recommend to my friends. I’m confident they’ll be benefited from this web site.

  19. Parse error: syntax error, unexpected ‘index’ (T_STRING), expecting ‘,’ or ‘;’ in C:\xampp\htdocs\login\process.php on line 19

  20. Hi there very nice site!! Guy .. Beautiful .. Amazing .. I will bookmark your web site and take the feeds additionally…I am glad to find numerous useful info right here in the submit, we’d like work out more strategies in this regard, thanks for sharing.

  21. Hello to every body, it’s my first pay a quick visit of this website; this website carries awesome and truly excellent data for visitors.|

  22. I¡¦ve recently started a site, the info you provide on this web site has helped me greatly. Thanks for all of your time & work.

  23. $myname = mysqli_real_escape_string($db,$_POST[‘nameg’]);
    for this,it is displaying Undefined variable: myname in C:\xampp\htdocs\gphp.php on line 15
    how to resolve this??

  24. Hey great post! I hope it’s alright that I shared it on my Twitter, if not,
    no problem just tell me and I’ll delete it. Regardless keep up the
    great work.

  25. Thanks , I have just been searching for information approximately
    this subject for a long time and yours is the greatest I have came upon till now.
    But, what in regards to the bottom line? Are you sure about the supply?

  26. I have read so many articles concerning the blogger lovers however this
    article is actually a nice article, keep it up.

  27. As I follow your instructions and steps as follows It worked well for me. If you face any issue in hp services you can visit us at our site.

  28. when i tried to run this is what happened. please help me with this.
    Parse error: syntax error, unexpected ‘>’, expecting end of file in C:\xampp\htdocs\project\process.php on line 2

  29. Your process.php has some problem, I think form php is not link with process.php. In process.php always show

    Notice: Undefined index: first_name in C:\xampp\htdocs\process.php on line 3

    Notice: Undefined index: email in C:\xampp\htdocs\process.php on line 5

  30. i too am a student . my localhost server doesnt processphp file rather it displaysall the code as it is .how to fix this .i will be grateful to who answer it .

  31. Hi albert, Greetings!

    If you properly created the table(employees1) inside the database(mydatabse) then it should work with below codding as per the nice guide lines given by the Author in this tutorial…….!!!

    Cheers!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -