ドットインストールの「PHPでTodo管理アプリを作ろう」という講座についての質問です
現在、この講座の12回まで終了したのですが、その講座中のプログラミングを全て打ち込み、仮装サーバーを使って読み込んでみると
「SQLSTATE[HY000] [1045] Access denied for user 'dbuser'@'localhost' (using password: YES)」
このようなエラーが出てしまいました。
自分で色々と調べたり考えたりしてみて以下の考察まではできました。
・どうやらユーザーネームに問題がありそうだ
・おそらくAJAXを導入する際に問題が生じた(導入前は問題なかった)
・プログラムについては特に問題がない
この考察が本当にあっているのかはわからないですが、これらを踏まえてもまだよくわかりません。
初心者なので、勘違いしている部分もあるかもしれないです。どうしてこのような問題が出てくるのか、そしてどのように解決すればいいのかを教えてください。
PHP
1//_ajax.php おそらくajaxを読み込むファイルかと 2<?php 3//全体的に見るとajaxを設定するための命令 4require_once(__DIR__ . '/config.php'); 5require_once(__DIR__ . '/function.php'); 6require_once(__DIR__ . '/Todo.php'); 7 8//get Todos 9$todoApp = new \MyApp\Todo(); 10 11if($_SERVER['REQUEST_METHOD'] === 'POST'){ 12 try{ 13 $res = $todoApp->post(); 14 header('Content-Type:application/json'); 15 echo json_encode($res); 16 exit; 17 }catch (Exception $e) { 18 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); 19 echo $e->getMessage(); 20 exit; 21 } 22} 23
PHP
1 2//todo.php 3様々な処理をするための命令? 4<?php 5 6namespace MyApp; 7 8class Todo{ 9 private $_db; 10 11 public function __construct(){ 12 //MySQLとPHPをつなげる魔法の言葉! 13 try{ 14 $this->_db = new \PDO(DSN, DB_USERNAME, DB_PASSWORD); 15 $this->_db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 16 } catch (\PDOException $e) { 17 echo $e->getMessage(); 18 exit; 19 } 20 } 21 //魔法の言葉はここまで 22 23 //全てのデータを配列として並び替える命令 24 public function getAll(){ 25 $stmt = $this->_db->query("select * from todos order by id desc"); 26 return $stmt->fetchAll(\PDO::FETCH_OBJ); 27 } 28 29 //これははTodoの記入等のデータベースを処理するメゾット 30 public function post(){ 31 //modeが渡って来なかったら例外を返す処理 32 if(!isset($_post['mode'])){ 33 throw new \Exception('mode not set!'); 34 } 35 36 switch ($_POST['mode']){ 37 case 'update'; 38 return $this->_update(); 39 case 'create'; 40 return $this->_create(); 41 case 'delete'; 42 return $this->_delete(); 43 } 44 } 45 46 private function _update(){ 47 //idが渡って来なかったら例外を返す処理 48 if(!isset($_post['id'])){ 49 throw new \Exception('[update] id not set!'); 50 } 51 52 $this -> _db -> beginTransaction(); 53 54 $sql = sprintf('update todos set state = (state+1) % 2 where id = %d', 55 $_POST['id']); 56 $stmt = $this->$_db->prepare($sql); 57 $stmt->excute(); 58 59 //stateを再び返す 60 $sql = sprintf('select state from todos where id = %d', 61 $_POST['id']); 62 $stmt = $this->$_db->query($sql); 63 $state = $stmt->fetchColumn(); 64 65 $this -> _db -> commit(); 66 67 //配列で返してあげる 68 return[ 69 'state' => $state 70 ]; 71 } 72 73 private function _create(){ 74 75 } 76 77 private function _delete(){ 78 79 } 80} 81 82 ?> 83
js
1$(function(){ 2 'use strict'; 3 4 $('#todos').on('click','.update_todo',function(){ 5 //idを取得 6 var id = $('this').parents('li').data('id'); 7 8 //ajax処理 9 $.post('_ajax.php',{ 10 id:id, 11 mode:"update" 12 },function(res){ 13 if(res.state === '1'){ 14 $('#todo_' + id).find('.todo_title').addClass('done') 15 }else{ 16 $('#todo_' + id).find('.todo_title').removeClass('done') 17 } 18 }) 19 }); 20 21}); 22
php
1//メインファイル 2<?php 3 4require_once(__DIR__ . '/config.php'); 5require_once(__DIR__ . '/function.php'); 6require_once(__DIR__ . '/Todo.php'); 7 8//get Todos 9$todoApp = new \MyApp\Todo(); 10$todos = $todoApp->getAll(); 11 12 ?> 13 14 <!DOCTYPE html> 15 <html lang="ja"> 16 <head> 17 <meta charset="utf-8"> 18 <title>My Todos</title> 19 <link rel = "stylesheet" href = "styles.css"> 20 </head> 21 <body> 22 <div id = "container"> 23 <h1>Todos</h1> 24 <form action=""> 25 <input id = "new_todo" type = "text" placeholder = "what needs to be done?"> 26 </form> 27 <ul id = "todos"> 28 <?php foreach ($todos as $todo) : ?> 29 <li id = "todo_<?= h($todo->id); ?>" data-id="<?= h($todo->id); ?>"> 30 <input type = "checkbox" class = "update_todo" <?php if($todo->state === '1') { echo 'checked'; } ?>> 31 <span class = "todo_title <?php if($todo->state === '1'){ echo 'done'; } ?>"> 32 <?= h($todo->title); ?></span> 33 <div class = "delete_todo">x</div> 34 </li> 35 <?php endforeach; ?> 36 </ul> 37 </div> 38 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 39 <script src = "todo.js"></script> 40 </body> 41 </html> 42### ヘディングのテキスト
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。