teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

issetのコード追記

2019/08/07 13:03

投稿

hello_mate
hello_mate

スコア21

title CHANGED
File without changes
body CHANGED
@@ -88,4 +88,75 @@
88
88
  </body>
89
89
  </html>
90
90
 
91
+ ```
92
+ ```php2
93
+ <?php
94
+ session_start();//セッション開始
95
+ require_once "./common_function.php";//common_function呼び込み
96
+ //DBハンドルの取得
97
+ $pdo = get_dbh();
98
+ $err_msg = array();
99
+
100
+ if($_SERVER['REQUEST_METHOD'] === 'POST'){
101
+ //メールアドレスとパスワードを取得
102
+ if(isset($_POST['account_email']) === TRUE){
103
+ $account_email = $_POST['account_email'];
104
+ }else{
105
+ $err_msg[] = 'メールアドレスが未入力です。';
106
+ }
107
+ if(isset($_POST['account_password']) === TRUE){
108
+ $account_password = $_POST['account_password'];
109
+ }else{
110
+ $err_msg[] = 'パスワードが未入力です。';
111
+ }
112
+ var_dump($err_msg);
113
+ //var_dump($account_email);
114
+ //var_dump($account_password);
115
+ if(count($err_msg) === 0){
116
+ try{
117
+ //sql実行
118
+ $sql = 'SELECT * FROM account Where account_email = :account_email and account_password = :account_password';
119
+ $stmt = $pdo->prepare($sql);
120
+ $stmt -> bindValue(':account_email', $account_email, PDO::PARAM_STR);
121
+ $stmt -> bindValue(':account_password', $account_password, PDO::PARAM_STR);
122
+ $stmt->execute();
123
+ //var_dump($stmt);
124
+ $count = $stmt -> rowCount();
125
+ if($count > 0){
126
+ header('Location: ./item_list.php');
127
+ }
128
+ //認証処理
129
+ }catch(PDOException $e){
130
+ echo $e->getMessage();
131
+ }
132
+ }
133
+ }
134
+ $pdo = null;
135
+ ?>
136
+ <!DOCTYPE html>
137
+ <html>
138
+ <head>
139
+ <title>ログイン画面</title>
140
+ <link rel="stylesheet" href="login.css">
141
+ </head>
142
+ <body>
143
+ <div class="wrapper">
144
+ <H1>ログイン画面</H1>
145
+ <?php foreach($err_msg as $value) {?>
146
+ <p><?php print $value ?></p>
147
+ <?php } ?>
148
+ <form action="item_list.php" method="POST">
149
+ <div class="insert-form">
150
+ <p class="email">メールアドレス</p>
151
+ <input type="email" name="account_email">
152
+ <p class="password">パスワード(8文字以上、時半角英数字のみ使用可能)</p>
153
+ <input type="password" name="account_password">
154
+ </div>
155
+ <input type="submit" name="login" value="ログイン">
156
+ </form>
157
+ <input type ="button" value="新規登録" onclick="location.href='./register.php'">
158
+ </div>
159
+ </body>
160
+ </html>
161
+
91
162
  ```

2

code内容変更しました

2019/08/07 13:03

投稿

hello_mate
hello_mate

スコア21

title CHANGED
File without changes
body CHANGED
@@ -18,10 +18,13 @@
18
18
 
19
19
  ----------------------------------
20
20
 
21
- すいませんがよろしくお願いします。
22
-
23
21
  ```php
22
+ <?php
23
+ session_start();//セッション開始
24
+ require_once "./common_function.php";//common_function呼び込み
25
+ //DBハンドルの取得
26
+ $pdo = get_dbh();
24
- $err_msg = array();
27
+ $err_msg = array();
25
28
 
26
29
  if($_SERVER['REQUEST_METHOD'] === 'POST'){
27
30
  //メールアドレスとパスワードを取得
@@ -36,13 +39,53 @@
36
39
  $err_msg[] = 'パスワードが未入力です。';
37
40
  }
38
41
  var_dump($err_msg);
42
+ //var_dump($account_email);
43
+ //var_dump($account_password);
39
-       if(count($err_msg) === 0){
44
+ if(count($err_msg) === 0){
45
+ try{
40
-        エラーメッセージが0であればこの後にSQL実行
46
+ //sql実行
47
+ $sql = 'SELECT * FROM account Where account_email = :account_email and account_password = :account_password';
48
+ $stmt = $pdo->prepare($sql);
49
+ $stmt -> bindValue(':account_email', $account_email, PDO::PARAM_STR);
50
+ $stmt -> bindValue(':account_password', $account_password, PDO::PARAM_STR);
51
+ $stmt->execute();
52
+ //var_dump($stmt);
53
+ $count = $stmt -> rowCount();
54
+ if($count > 0){
55
+ header('Location: ./item_list.php');
41
-       }
56
+ }
57
+ //認証処理
58
+ }catch(PDOException $e){
59
+ echo $e->getMessage();
42
-      }
60
+ }
43
-
61
+ }
62
+ }
63
+ $pdo = null;
64
+ ?>
65
+ <!DOCTYPE html>
66
+ <html>
67
+ <head>
68
+ <title>ログイン画面</title>
69
+ <link rel="stylesheet" href="login.css">
70
+ </head>
71
+ <body>
72
+ <div class="wrapper">
44
- <H1>ログイン画面</H1>
73
+ <H1>ログイン画面</H1>
45
74
  <?php foreach($err_msg as $value) {?>
46
75
  <p><?php print $value ?></p>
47
76
  <?php } ?>
77
+ <form action="item_list.php" method="POST">
78
+ <div class="insert-form">
79
+ <p class="email">メールアドレス</p>
80
+ <input type="email" name="account_email">
81
+ <p class="password">パスワード(8文字以上、時半角英数字のみ使用可能)</p>
82
+ <input type="password" name="account_password">
83
+ </div>
84
+ <input type="submit" name="login" value="ログイン">
85
+ </form>
86
+ <input type ="button" value="新規登録" onclick="location.href='./register.php'">
87
+ </div>
88
+ </body>
89
+ </html>
90
+
48
91
  ```

1

行いたい動作の詳細を追記させていただきました。

2019/08/07 12:51

投稿

hello_mate
hello_mate

スコア21

title CHANGED
File without changes
body CHANGED
@@ -2,8 +2,22 @@
2
2
  エラーメッセージを取得できずに苦戦しています。
3
3
  empty部分をisesetなどに変えて試しましたがうまく動作しませんでした。
4
4
 
5
+ --------------------------
6
+ POSTされてきた値が空だった場合に
7
+ err_msgに格納していき
5
- どこの書き方が動作の不備になっているのでしょうか?
8
+ html部分でforeachで未入力情報を書き出す。
6
9
 
10
+ と、php部分でerr_msgに配列が渡されていたら
11
+ if(count($err_msg) === 0){
12
+        エラーメッセージが0であればこの後にSQL実行文
13
+       }
14
+ 以後の処理には行かせないようにしたいと思っています。
15
+
16
+ ですが、フォーム画面で未入力のままログインボタンを押すと
17
+ エラー表示が出ずに次ページへ遷移されてしまいます。
18
+
19
+ ----------------------------------
20
+
7
21
  すいませんがよろしくお願いします。
8
22
 
9
23
  ```php