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

質問編集履歴

3

誤字

2020/03/11 02:30

投稿

yoto009
yoto009

スコア9

title CHANGED
File without changes
body CHANGED
@@ -47,14 +47,15 @@
47
47
 
48
48
 
49
49
  # パスワードを持っていないユーザーを取得してパスワード発行
50
- $stmt = $this->pdo->prepare('
50
+ $sql = '
51
- UPDATE `users`
51
+ UPDATE users
52
- SET `user_pw` = :pass
52
+ SET pass = :pass
53
- WHERE `user_pw` = :user_pw
53
+ WHERE user_id = :user_id
54
- ');
54
+ ';
55
+ $prepare = $dbn->prepare($sql);
55
- $stmt->bindValue(':pass', $pass);
56
+ $prepare->bindValue(':pass', $pass);
56
- $stmt->bindValue(':user_pw', '');
57
+ $prepare->bindValue(':user_id', '');
57
- $stmt->execute();
58
+ $prepare->execute();
58
59
  ```
59
60
  # 追記
60
61
  これでランダム文字列をhash化して初期値の設定はできました。

2

内容の追記

2020/03/11 02:30

投稿

yoto009
yoto009

スコア9

title CHANGED
File without changes
body CHANGED
@@ -10,34 +10,34 @@
10
10
  ※下記のusersテーブルでパスワードを持っていないuser_id毎に違うパスワードを発行したい
11
11
  |user_id|user_pw|user_name|
12
12
  |:--|:--|:--|
13
- |1||佐藤一郎|
13
+ |1||ユーザー1|
14
- |2||鈴木二郎|
14
+ |2||ユーザー2|
15
- |3||田中三郎|
15
+ |3||ユーザー3|
16
- |4||阿部四郎|
16
+ |4||ユーザー4|
17
- |5||高橋五郎|
17
+ |5||ユーザー5|
18
18
 
19
19
  ⬇現在の状態
20
20
  |user_id|user_pw|user_name|
21
21
  |:--|:--|:--|
22
- |1|123abc|佐藤一郎|
22
+ |1|12345678|ユーザー1|
23
- |2|123abc|鈴木二郎|
23
+ |2|12345678|ユーザー2|
24
- |3|123abc|田中三郎|
24
+ |3|12345678|ユーザー3|
25
- |4|123abc|阿部四郎|
25
+ |4|12345678|ユーザー4|
26
- |5|123abc|高橋五郎|
26
+ |5|12345678|ユーザー5|
27
27
 
28
28
  ⬇理想の形
29
29
  |user_id|user_pw|user_name|
30
30
  |:--|:--|:--|
31
+ |1|12345678|ユーザー1|
31
- |1|123abc|佐藤一郎|
32
+ |2|1234abcd|ユーザー2|
32
- |2|1234xz|鈴木二郎|
33
- |3|abc987|田中三郎|
33
+ |3|abcd1234|ユーザー3|
34
- |4|2ab678|阿部四郎|
34
+ |4|5678abcd|ユーザー4|
35
- |5|012xyz|高橋五郎|
35
+ |5|abcdefgh|ユーザー5|
36
36
 
37
37
  # 該当のソースコード
38
38
  ```php
39
39
  # パスワード発行用
40
- $num = 6;
40
+ $num = 8;
41
41
  $ar1 = range('a','z');
42
42
  $ar2 = range('A','Z');
43
43
  $ar3 = range('0','9');
@@ -55,4 +55,21 @@
55
55
  $stmt->bindValue(':pass', $pass);
56
56
  $stmt->bindValue(':user_pw', '');
57
57
  $stmt->execute();
58
+ ```
59
+ # 追記
60
+ これでランダム文字列をhash化して初期値の設定はできました。
61
+ ```php
62
+ foreach($user as $user_id) {
63
+ $pass = substr(bin2hex(random_bytes($num)), 0, $num);
64
+ $hash = password_hash($pass, PASSWORD_BCRYPT);
65
+ $sql = '
66
+ UPDATE users
67
+ SET user_pw = :user_pw
68
+ WHERE user_id = :user_id
69
+ ';
70
+ $prepare = $dbn->prepare($sql);
71
+ $prepare->bindValue(':user_pw', $hash);
72
+ $prepare->bindValue(':user_id', $user_id);
73
+ $prepare->execute();
74
+ }
58
75
  ```

1

誤字

2020/03/11 02:22

投稿

yoto009
yoto009

スコア9

title CHANGED
File without changes
body CHANGED
@@ -50,9 +50,9 @@
50
50
  $stmt = $this->pdo->prepare('
51
51
  UPDATE `users`
52
52
  SET `user_pw` = :pass
53
- WHERE `user_id` = :user_pw
53
+ WHERE `user_pw` = :user_pw
54
54
  ');
55
- $stmt->bindParam(':pass', $pass);
55
+ $stmt->bindValue(':pass', $pass);
56
- $stmt->bindParam(':user_pw', '');
56
+ $stmt->bindValue(':user_pw', '');
57
57
  $stmt->execute();
58
58
  ```