简体   繁体   中英

SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' when uploading to PhpMyAdmin database table

Firstly, I am quite new to PHP so there may be errors in my code which aren't related to this question (which may have been answered elsewhere).

The problem : I am trying to upload a prepared statement into a table in PhpMyAdmin (XAMPP), hosted locally, to create user accounts. Whenever I try to create a new user I receive this message...

Error: SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to 
database 'coursework,root'

I have already tried multiple solutions that worked for others such as changing my servername to 127.0.0.1 from localhost and checking

$cfg['Servers'][$i]['AllowNoPassword'] = true;

is set to true for both my config.inc.php and config.sample.inc files. However, this has not change the result.

This is the code in the config.inc.php file

$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Lang'] = '';

Here's my code for my php file

<?php

$msg = "";
$servername = "localhost";
$username = "root";
$password = '';
$dbname = "coursework";

if (isset($_POST['RegisterBtn'])) {

    $Password = ($_POST['PasswordInput']);
    $CPassword = ($_POST['ConfirmPasswordInput']);

    if ($Password != $CPassword) {
        $msg = ("Passwords do not match, Please try again");
    } else {

        try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname,$username,$password");

            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            //Preparing the sql and binding new account parameters
            $statement = $conn->prepare("INSERT INTO userlogin (Forname,Surname,Email_Address,Password)VALUES(:Forname,:Surname,:Email_Address,:Password)");
            $statement->bindParam(':Forename', $Forename);
            $statement->bindParam(':Surname', $Surname);
            $statement->bindParam(':Email_Address', $EmailAddress);
            $statement->bindParam(':Password', $hash);

            //inserting user data
            $Forename = ($_POST['ForenameInput']);
            $Surname = ($_POST['SurnameInput']);
            $EmailAddress = ($_POST['EmailInput']);
            $hash = password_hash($Password, PASSWORD_BCRYPT);

            //executing the statement
            $statement->execute();
            $msg = "Your account has been created...";

        } catch (PDOException $e) {
            $msg = "Error: " . $e->getMessage();
        }
    }
}
$conn = null;
?>

Any answers that aid my understanding are appreciated. Thanks!

this line is wrong.

$conn = new PDO("mysql:host=$servername;dbname=$dbname,$username,$password");

Try this.

$conn = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM