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

Problems with the Windows version of XAMPP, questions, comments, and anything related.

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

Postby abc1000000 » 13. February 2023 12:12

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!!
abc1000000
 
Posts: 12
Joined: 13. February 2023 12:07
XAMPP version: 3.3.0
Operating System: Windows

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby Altrea » 14. February 2023 11:22

Hi,

Did you create the database user first? (e.g. in phpmyadmin)?

Can you login the database user in the MariaDB command line interface (CLI)?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11936
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby abc1000000 » 14. February 2023 11:55

Hi guys - upon advice elsewhere I was informed the problem was that my root MySQL user had a password. I couldn't remember what that was so I uninstalled and reinstalled MySQL, created a password for the root user using password manager, and added that password to the php code. I now 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


the staff database that I created a couple of days ago (in phpMyAdmin) appears still to be there.

Image

what do I need to do to get things working?

Thank you!
abc1000000
 
Posts: 12
Joined: 13. February 2023 12:07
XAMPP version: 3.3.0
Operating System: Windows

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby Altrea » 14. February 2023 13:29

can you access the database staff with the root user credentials?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11936
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby abc1000000 » 14. February 2023 14:04

Sorry to be dense - I am very new to this. I tried following the steps in the various posts on the internet and have taken introductory classes to PHP, MySQL etc. on Codedacemy but don't fully understand what you mean. I can see staff in http://localhost/phpmyadmin/index.php?route=/database/structure&db=staff - I can't see it in MySQL Workbench. I don't really know what is stored where or really how all of this works - I was hoping to just get the simple HTML --> PHP --> MySQL code working and build up from there, adapting the code to work with my own forms, then doing some interesting stuff in SQL with it, using that to produce custom HTML pages, and at some point migrating this from localhost to an actual internet page. Thus far I'm not getting far, it's so frustrating I'm tempted to give up, but I will persist and succeed. But yeah, massively appreciate your help getting this set up. Thanks.
abc1000000
 
Posts: 12
Joined: 13. February 2023 12:07
XAMPP version: 3.3.0
Operating System: Windows

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby Altrea » 14. February 2023 15:01

Something seems to be wrong with your staff database.
That it is listed in the left column does not mean anything.


can you click it? can you see the structure of it (if any structure existing)?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11936
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby abc1000000 » 14. February 2023 16:30

Below is what happens when I click on staff and then when I click on college which is the table the code is trying to write to.

This may or may not be relevant but when, over the weekend, I first started up XAMPP - Apache started with no problem but MySQL wouldn't connect. After a little googling I changed the port from 3306 to 3307 and it connected. Was this to do with MySQL's root user having a password? Could this be causing the current problems?

Image

Image
abc1000000
 
Posts: 12
Joined: 13. February 2023 12:07
XAMPP version: 3.3.0
Operating System: Windows

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby Altrea » 14. February 2023 23:02

in your screenshot i can see that phpmyadmin is connected wirh database management system on port 3307. die your own script connect to port 3307 to?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11936
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby abc1000000 » 15. February 2023 00:11

Code: Select all
<!DOCTYPE html>
<html>
 
<head>
    <title>Insert Page page</title>
</head>
 
<body>
    <center>
        <?php
 
        // servername => localhost
        // username => root
        // password => empty
        // database name => staff
        $conn = mysqli_connect("localhost", "root", "thecorrectpassword", "staff");
         
        // Check connection
        if($conn === false){
            die("ERROR: Could not connect. "
                . mysqli_connect_error());
        }
         
        // Taking all 5 values from the form data(input)
        $first_name =  $_REQUEST['first_name'];
        $last_name = $_REQUEST['last_name'];
        $gender =  $_REQUEST['gender'];
        $address = $_REQUEST['address'];
        $email = $_REQUEST['email'];
         
        // Performing insert query execution
        // here our table name is college
        $sql = "INSERT INTO college  VALUES ('$first_name',
            '$last_name','$gender','$address','$email')";
         
        if(mysqli_query($conn, $sql)){
            echo "<h3>data stored in a database successfully."
                . " Please browse your localhost php my admin"
                . " to view the updated data</h3>";
 
            echo nl2br("\n$first_name\n $last_name\n "
                . "$gender\n $address\n $email");
        } else{
            echo "ERROR: Hush! Sorry $sql. "
                . mysqli_error($conn);
        }
         
        // Close connection
        mysqli_close($conn);
        ?>
    </center>
</body>
 
</html>


and not that it should matter I think but here is the html

Code: Select all
<!DOCTYPE html>
<html lang="en">
   <head>
      <title>GFG- Store Data</title>
   </head>
   <body>
      <center>
         <h1>Storing Form data in Database</h1>
         <form action="insert.php" method="post">
             
<p>
               <label for="firstName">First Name:</label>
               <input type="text" name="first_name" id="firstName">
            </p>
 
             
<p>
               <label for="lastName">Last Name:</label>
               <input type="text" name="last_name" id="lastName">
            </p>
 
             
<p>
               <label for="Gender">Gender:</label>
               <input type="text" name="gender" id="Gender">
            </p>
 
             
<p>
               <label for="Address">Address:</label>
               <input type="text" name="address" id="Address">
            </p>
 
             
<p>
               <label for="emailAddress">Email Address:</label>
               <input type="text" name="email" id="emailAddress">
            </p>
 
            <input type="submit" value="Submit">
         </form>
      </center>
   </body>
</html>
abc1000000
 
Posts: 12
Joined: 13. February 2023 12:07
XAMPP version: 3.3.0
Operating System: Windows

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby abc1000000 » 15. February 2023 11:36

If that's the problem I'm happy either to change the code so it connects on port 3307 (surprisingly hard to find a straightforward answer on how to do this) or, if there's a way to get XAMPP working on port 3306 - that might be easier? Thanks.
abc1000000
 
Posts: 12
Joined: 13. February 2023 12:07
XAMPP version: 3.3.0
Operating System: Windows

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby Nobbie » 15. February 2023 11:59

abc1000000 wrote:surprisingly hard to find a straightforward answer on how to do this


No, its not. Simply read the docu about mysqli_connect(). See the 5th parameter (which you omitted).
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby abc1000000 » 15. February 2023 12:14

I changed line 16 to

Code: Select all
       $conn = mysqli_connect("localhost", "root", "thecorrectpassword", "staff", 3307);


and went back to getting something similar to the previous error. (also tried it with 3307 in quote marks)

Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: YES) 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', 3307) #1 {main} thrown in C:\xampp\htdocs\GeeksforGeeks\insert.php on line 16


removing the port gives

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


So frustrating. Thanks for all your patience.
abc1000000
 
Posts: 12
Joined: 13. February 2023 12:07
XAMPP version: 3.3.0
Operating System: Windows

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby abc1000000 » 16. February 2023 00:43

Sorry to be a pain about something so boring and trivial but I'm really keen to get this going so I can test out a basic website on a home server and build things from there. Need to be to send a form to a database before building out more functionality really. I really am trying. Thanks.
abc1000000
 
Posts: 12
Joined: 13. February 2023 12:07
XAMPP version: 3.3.0
Operating System: Windows

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby Altrea » 16. February 2023 02:20

abc1000000 wrote:I changed line 16 to

Code: Select all
       $conn = mysqli_connect("localhost", "root", "thecorrectpassword", "staff", 3307);


and went back to getting something similar to the previous error. (also tried it with 3307 in quote marks)

Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: YES) 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', 3307) #1 {main} thrown in C:\xampp\htdocs\GeeksforGeeks\insert.php on line 16


removing the port gives

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


So frustrating. Thanks for all your patience.

You need to realize one thing:
You are running currently two MariaDB or mysql database installations, one is listening on default port 3306 and one is listening on custom port 3307.
One does have a staff database, one does not
One does have the root password you tried in your script, one does not.

Just your two installations are confusing you. I can see that this can be frustrating, but XAMPP does not tell you to have another MariaDB / MySQL installed or change the port to custom 3307.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11936
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Error setting up simple HTML -> PHP -> MySQL on local se

Postby abc1000000 » 16. February 2023 10:07

Thanks. Should I delete the original MySQL? Should I delete the original MySQL *and uninstall/reinstall XAMPP?

edit: I realise I could just do this but I'd rather check worse less I do something that makes things worse.
abc1000000
 
Posts: 12
Joined: 13. February 2023 12:07
XAMPP version: 3.3.0
Operating System: Windows

Next

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 164 guests