How to connect MySQL database with HTML form

Hi, I hope I can get the answer that I need here.

So, I’m making a very simple HTML form that can input just a username and a password. I have created a database with 2 collumns for both field, username & password. I have also created both the HTML file and the PHP file. It has a submit button, but when I click on it, it won’t input anything to the database. Instead I get the error message as shown below.

This is my HTML page code:

<!DOCTYPE html>
<title>Form site</title>
<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">

This is my PHP script:

$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
$host = "";
$dbusername = "epiz_31851316";
$dbpassword = "PASSWORDHERE";
$dbname = "epiz_31851316_youtube";
// Create connection
$conn = new mysqli ($, $epiz_31851316, $PASSWORDHERE, $epiz_31851316_youtube);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '
. mysqli_connect_error());
$sql = "INSERT INTO account (username, password)
values ('$username','$password')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
echo "Error: ". $sql ."
". $conn->error;
echo "Password should not be empty";
echo "Username should not be empty";

How do I connect the database with the HTML form? Where do I input my host name, database name, username and password in the PHP script?

The host, database name, username and password are all supposedly correct, just as provided on the MySQL Details and home vp page.

### Username


### Error Message

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(); } ?>

### Other Information

Any input/help would be much appreciated. Thank you.

  1. You are missing a quotation mark on the line you define the db password

  2. You are using variables that are not set when you create the connection to the database

  3. NEVER enter a plain text password into the database. If you get hacked, you are basically handing out your users password to the hackers, and probably asking for a lawsuit as well.


For the quotation mark, that’s just a bad copy & pasting. It actually has a quotation mark on the script. As for my password, that is not my actual password. I use the password provided by Infinity Free.

Can you please copy and paste the code exactly how it appears in your code then? Or upload the file to a sharing service like Google drive and share the link here.


What error message is that exactly? Your code has a couple of different error messages it can show. Please tell us the actual error message you see.

Under “error message” you just display what is basically just the error handling code. Is that actually what you see? Because if so, that’s definitely a code syntax error, not a database error.


Sorry for not being clear enough. Yes, text under “error message” is the error I’m talking about.

As far as I know, it should show the text “New record is inserted sucessfully” if the process is successful. Why is it showing multiple error text instead?

If I click on submit, the text that comes up is like this:

The exact code is just like the ones above, except that I forgot to put back the quotation mark as I was hiding my actual password.

The only way you would see that output if in the code if ($conn->query($sql)){ something causes PHP to just stop executing and print the rest of the code as output. That’s a syntax error.

The only clear code error I see in your shared snippet is this one (using option values as variable names? either use the variables you created above or use the variable values as input directly):

But I don’t see how that could cause PHP to print the code a few lines down.

I took a look at your account to make sure are really looking at the latest version of the code, but I couldn’t find any PHP code there. Did you actually upload this?


No, I haven’t uploaded the file yet. I wanna make sure that it works first locally, then I can test it on the server.

Just now, I uploaded the files to see if it works or not. Unfortunately I get the error “Connect Error (2002) Connection timed out” after clicking on Submit.

Well that’s why its not working

Upload your code


I have already. It gives me the “Connect Error (2002) Connection timed out” error.

You said you dident upload your code, now you have?

Please share the exact URL of the file you uploaded.



HTML Form:

I get the message “username should not be empty”. Probably because you are using variables that you never defined when establishing the connection to the DB.


Not when you try to fill and submit the form. It will show: Connect Error (2002) Connection timed out.

I’m getting more and more confused now. If I can’t figure out what’s wrong with it in the next few days, I might as well have to consider dropping the form project. It’s taking a bit too much time to figure it out.

If I just visit the PHP file, it tries to just connect to the database.

Looking at your code, and the error I got, your code is incorrect. You can’t use your credentials as variables, you need to use the variables you defined.


OH MY GOD! Sorry for being so excited.

Now, I understand where I have gone wrong. It works. I learnt a lot about making a very simple form and PHP script from just this thread.

Thank you so much to everyone who has helped me.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.