Jump to content

Error setting up simple HTML -> PHP -> MySQL on local server


Recommended Posts

Hi. I'm looking to set up some simple code that sent data submitted on a forms to a database on a website and it's not so simple! I thought I'd start with doing it on a local server then move that to online but I keep running in to an error.

I' tried to set up a simple html->php->MySQL page using the code here:

https://www.raghwendra.com/blog/how-to-connect-html-to-database-with-mysql-using-php-example/

unfortunately when submitting form data to test I get the following error message

"Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: NO) in C:\xampp\htdocs\contact\contact.php:5 Stack trace: #0 C:\xampp\htdocs\contact\contact.php(5): mysqli_connect('localhost', 'root', Object(SensitiveParameterValue), 'db_contact') #1 {main} thrown in C:\xampp\htdocs\contact\contact.php on line 5"

I also tried setting up a simple html->php->MySQL page using the code on 

https://www.webcodzing.com/connect-html-form-to-mysql-database-using-php/ 

which is much the same but with the database being password protected. That time I got the following error code:

Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'formdb_user'@'localhost' (using password: YES) in C:\xampp\htdocs\testsite\form.php:22 Stack trace: #0 C:\xampp\htdocs\testsite\form.php(22): mysqli_connect('localhost', 'formdb_user', Object(SensitiveParameterValue), 'form_entriesdb') #1 {main} thrown in C:\xampp\htdocs\testsite\form.php on line 22

How do I resolve this? I'm finding this all very frustrating and just want to know how to fix it. Thank you!!

Link to comment
Share on other sites

both errors are access permission problems. in the 1st case, using the root user w/o a password, simply creating a database and table should have  automatically given the root user permission to access that database. did you use phpmyadmin to create a database at all, then the database table?

Link to comment
Share on other sites

Hi Mac, thank you. I followed the instructions in those urls. I downloaded and installed XAMPP. I had downloaded and installed mysql, phpMyAdmin and mysql workbench but that was previous. Is it possible that's the issue? I really thought this would be a lot easier - I'm just trying to set up the most basic html to php to mysql system possible and then play around from there. I can't even do that, so am unable to make any progress with my project. It's incredibly frustrating. I've spent hours on it but nothing.

Link to comment
Share on other sites

2 hours ago, abc1000000 said:

Hi Mac, thank you. I followed the instructions in those urls. I downloaded and installed XAMPP. I had downloaded and installed mysql, phpMyAdmin and mysql workbench but that was previous. Is it possible that's the issue? I really thought this would be a lot easier - I'm just trying to set up the most basic html to php to mysql system possible and then play around from there. I can't even do that, so am unable to make any progress with my project. It's incredibly frustrating. I've spent hours on it but nothing.

FYI I also tried following https://www.geeksforgeeks.org/how-to-insert-form-data-into-database-using-php/ and got a similar looking error - 

"Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: NO) in C:\xampp\htdocs\GeeksforGeeks\insert.php:16 Stack trace: #0 C:\xampp\htdocs\GeeksforGeeks\insert.php(16): mysqli_connect('localhost', 'root', Object(SensitiveParameterValue), 'staff') #1 {main} thrown in C:\xampp\htdocs\GeeksforGeeks\insert.php on line 16"

 FWIW line 16 is "$conn = mysqli_connect("localhost", "root", "", "staff");"

I'm just so frustrated. Once I get this set up I can experiment with more complicated forms, grabbing the data from them and doing funky stuff with it etc. I'll make something amazing. Right now I can't get it set up to receive someone's name, and am stuck.

Link to comment
Share on other sites

I'm not going to look at some old xampp tutorials and try and debug why they don't work, when it's pretty clear the answer is that the mysql root user has a password.  

Local installs like xampp have fallen out of favor for many reasons.  People used to use vagrant, and now use docker.  Either one would be better, not to mention closer to the environment into which you will deploy a finished app.

MySQL has changed its password encoding schemes over the years, so if it's an old tutorial, it likely doesn't account for these changes.

I realize it is localhost but it's just a bad habit to get into depending on passwordless database accounts.

It's also a bad habit to use the root mysql user for non-administrative tasks.

  • Figure out how to connect to the mysql database locally
    • Typically this would be either through the mysql command line tool  "mysql" or possibly through an installed phpMyAdmin app that xampp sets up for you.

Here's what you should do through either one of those:

  • create new database for your development
  • create new mysql user with password and grant permissions to the new database for that user

Update your connection code to use the user/password combination.

 

One thing to understand about mysql that is non-obvious is that its security scheme is based on both user and host.  

grant all privileges on mydb.* to 'myuser'@'localhost' identified by 'mypasswd';

This assumes the root mysql user was used to create a database named mydb.

Take careful note that these are single quotes, and not backtics.  Notice the pattern of the quoting for the username.  This command creates the user and sets the password and grants access to the database.

Once you know you can connect to the database with either mysql cli tool or phpMyAdmin using those credentials you can expect your PHP code to work.

Link to comment
Share on other sites

Thanks. I'm going to go for some exercise and try that in an hour or so.

Why would the mysql root user have a password account? I absolutely didn't add one after installing xampp - would this be a holdover from a previous install of mysql?

Thanks again!

Link to comment
Share on other sites

Some steps forward and some steps back.

 

I uninstalled and reinstalled MySQL and added a password created by a password manager. I then inserted that password into the code, and tried to submit some data into the form. It appears the password issue is gone, but it doesn't recognise the database. I get the following error

Fatal error: Uncaught mysqli_sql_exception: Unknown database 'staff' in C:\xampp\htdocs\GeeksforGeeks\insert.php:16 Stack trace: #0 C:\xampp\htdocs\GeeksforGeeks\insert.php(16): mysqli_connect('localhost', 'root', Object(SensitiveParameterValue), 'staff') #1 {main} thrown in C:\xampp\htdocs\GeeksforGeeks\insert.php on line 16

In phpMyAdmin the database 'staff' appears to exist. What do I need to do to get everything working?

Thank you!

image.thumb.png.ce49e88cef09bfeef54266fc4ac67c7a.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.