connect HTML Form to MySQL Database with PHP

Viewed 54

I have a block of code to submit my HTML form values to my Database using PHP via XAMPP. After construction and testing the record will saved in database successfully but I see a notice that says below:

Undefined variable: localhost in C:\xampp\htdocs\30-11-2021\processhtml.php on line 19

Kindly see my line 19 below also:

$conn = new mysql ($localhost, $dbusername, $dbpassword, $dbname);
1 Answers

You want

$conn =  new mysql ('localhost', $dbusername, $dbpassword, $dbname);

The first parameter is the hostname of the server running MySQL. In your case it's 'localhost'.

php warns you when you use an undefined variable, and gives the value null for that variable. In this context, a null hostname defaults to localhost, so that's why your code actually works.

Related