How to connect HTML Form to MySQL Database using PHP in 4 Minutes?

Want to know, How to connect HTML Form to MySQL Database with PHP. Here I have easy and Short steps to Insert Form data into a database.

Database

Now first, we need to create a MySQL database but if you don’t know how to create a database then check out How to create Database and Table in MySQL

Database Name = youtube
Table Name = account
Attribute = (id : int, username : varchar, password : varchar)

HTML Form Code

<!DOCTYPE html>
<html>
<head>
<title>Form site</title>
</head>
<body>
<form method="post" action="connect.php">
Username : <input type="text" name="username"><br><br>
Password : <input type="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

In the above HTML code, we use action="connect.php" that means we are sending data from our HTML form to connect.php.

How to connect HTML Form to MySQL Database
Your HTML form looks like this

PHP Code (connect.php)

<?php
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "youtube";
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);


if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '
. mysqli_connect_error());
}
else{
$sql = "INSERT INTO account (username, password)
values ('$username','$password')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
}
else{
echo "Error: ". $sql ."
". $conn->error;
}
$conn->close();
}
}
else{
echo "Password should not be empty";
die();
}
}
else{
echo "Username should not be empty";
die();
}
?>

Explaining connect.php

Line 2, 3: Grab a Form data and store it in  $username and $password .

4, 5: Check, whether this username and password are empty or not. If they are not empty then we can continue a process.

6-11: Making a database connection

13-15: Check, is their error while connecting the database. If yes then show that error message and exit. Otherwise, continue a process.

18, 19: SQL query, to insert data into a database table.

20, 21: If our insert query is executed, then give a message “New record is inserted successfully”

23-25: If the query fails to execute then show an error message.

At last, we close the database connection.

And this is how we connect HTML Form to MySQL Database using PHP.

And also learn, How to Display Data from MySQL Database into HTML Table using PHP

Also, watch this video.

5 1 vote
Article Rating
Subscribe
Notify of
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
vishal cougule
vishal cougule
1 year ago

how to add 4 values in database. like = clg name, city , email, number.

David Charles
David Charles
1 year ago

hello after evreything i get a error Parse error: syntax error, unexpected ‘>’ in C:xampphtdocstutoriallconnect.php on line 68

Anthony McCall
Anthony McCall
Reply to  David Charles
1 year ago

I got this as well. Did you ever fix this issue?

Abdullahi Nasiru
Abdullahi Nasiru
1 year ago

I got this error after everything

query($sql)){
echo “New record is inserted sucessfully”;
}
else{
echo “Error: “. $sql .”
“. $conn->error;
}
$conn->close();
}
}
else{
echo “Password should not be empty”;
die();
}
}
else{
echo “Username should not be empty”;
die();
}
?>

빨리PALLI Channel
빨리PALLI Channel
Reply to  Abdullahi Nasiru
4 months ago

same here

ananomous
11 months ago

can you share what code editor you use?
(cannot find anything like it)
and the download link pls

ananomous
11 months ago

can you share what code editor you use?
(cannot find anything like it)
and the download link pls

Placide
Placide
5 months ago

Sir,
Thank you for your clear, easy to follow tutorials.
Are you by chance accepting projects?
Thanks