前提・実現したいこと
データベース照合の際に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\Job\web\login.php:31 Stack trace: #0 C:\xampp\htdocs\Job\web\login.php(31): PDO->__construct('mysql:dbname=Jo...', 'root', 'password') #1 {main} thrown in C:\xampp\htdocs\Job\web\login.php on line 31
該当のソースコード
<?php require_once(dirname(__FILE__).'/../config/config.php'); if($_SERVER['REQUEST_METHOD'] == 'POST'){ //POST処理時 //1.入力値を取得 $user_no = $_POST['user_no']; $password = $_POST['password']; //echo $user_no.'<br>'; //echo $password; //exit; //2.入力値チェック $err = array(); if(!$user_no){ $err['user_no'] = '社員番号を入力してください。'; } if(!$password){ $err['password'] = 'パスワードを入力してください。'; } //var_dump($err); if(empty($err)) { //3.データベースに照合 $param = 'mysql:dbname='.DB_NAME.';host='.DB_HOST; $pdo = new PDO($param,DB_USER,DB_PASSWORD); $pdo->query('SET NAMES utf8;'); $sql = "SELECT user_no,name FROM user WHERE user_no = :user_no AND password = :password LIMIT 1"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':user_no',$user_no,PDO::PARAM_STR); $stmt->bindValue(':password',$password,PDO::PARAM_STR); $stmt->execute(); $user = $stmt->fetch(); var_dump($user); exit; //4.ログイン処理 //5.HOME画面へ遷移 } } else { //画面初回アクセス時 $user_no = ""; $password = ""; } ?> <!doctype html> <html lang="ja"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1", "shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384- 9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> <!-- Original CSS --> <link href="/css/style.css" rel="stylesheet" > <title>Job</title> </head> <body class="text-center bg-light"> <form class="border rounded bg-white form-login" method="POST"> <h1 class="h3 my-3">Login</h1> <div class="form-group pt-3"> <input type="text" class="form-control rounded-pill<?php if(isset($err['user_no'])) echo 'is-invalid';?>" name="user_no" placeholder="社員番号"> <div class="invalid-feedback"><?=$err['user_no']?></div> </div> <div class="form-group"> <input type="password" class="form-control rounded-pill<?php if(isset($err['password'])) echo 'is-invalid';?>" name="password" placeholder="パスワード"> <div class="invalid-feedback"><?=$err['password']?></div> </div> <button type="submit" class="btn btn-primary text-white rounded-pill px-5 my-4">ログイン</button> </form> <!-- Optional JavaScript; choose one of the two! --> <!-- Option 1: Bootstrap Bundle with Popper --> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/ zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/ tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script> <!-- Option 2: Separate Popper and Bootstrap JS --> <!-- <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.6.0/dist/umd/popper.min.js" integrity="sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.min.js" integrity="sha384-nsg8ua9HAw1y0W1btsyWgBklPnCUAFLuTMS2G72MMONqmOymq585AcH49TLBQObG" crossorigin="anonymous"></script> --> </body> </html>
補足情報(FW/ツールのバージョンなど)
エディタ:ATOM
データベース:phpmyadmin
データベースに挿入した文
INSERT INTO 'user'('id','user_no','name','password') VALUES (NULL,'1','名前','password');