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

質問編集履歴

1

PHPのコードをコピペミスしていたので追記

2016/04/08 07:26

投稿

MH00214
MH00214

スコア53

title CHANGED
File without changes
body CHANGED
@@ -87,34 +87,42 @@
87
87
  //SQL実行
88
88
  $flag = $stmt->execute();
89
89
 
90
-
91
- $id = '';
92
-
93
- $inputname = '';
94
-
95
- $inputemail = '';
96
-
97
90
  //エラー処理
98
91
  if($flag == false){
99
92
  echo "SQLエラー";
100
93
  }else{
101
- //これだと最後のデータしかinputタグに挿入されない。
102
- while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
103
- $id = $result['id'];
104
- $inputname .= $result['name'];
105
- $inputemail .= $result['email'];
106
- }
107
94
 
108
95
  $result = $stmt->fetchAll();
109
-
96
+ //$resultからidの配列を取り出す
110
97
  $id = array_column($result,'id');
111
- //id番号までは得できた
98
+ //$resultからnameの配列をり出す
99
+ $name = array_column($result,'name');
112
- echo $id[0];
100
+ //$resultからemailの配列を取り出す
101
+ $email = array_column($result,'email');
113
102
 
114
103
  }
115
104
 
116
105
  ?>
117
106
 
107
+ <!DOCTYPE html>
108
+ <html lang="ja">
109
+ <head>
110
+ <meta charset="UTF-8">
111
+ <title>Document</title>
112
+
113
+ </style>
114
+ </head>
115
+ <body>
116
+
117
+ <form action="dataupdate.php" method="post">
118
+ <!-- $name[0] のときはdetail.php?id=1 のinputタグに入れたい-->
119
+ <input type="text" name="name" value="<?php echo $name[0]; ?>">
120
+ <!-- $email[0] のときはdetail.php?id=1 のinputタグに入れたい-->
121
+ <input type="text" name="email" value="<?php echo $email[0]; ?>">
122
+ <input type="submit" value="更新する">
123
+ </form>
124
+ </body>
125
+ </html>
118
126
  ```
119
127
 
120
128
  ###試したこと