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

質問編集履歴

4

修正前と修正後のコードを書きました

2017/04/18 08:08

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
 
11
11
  ###該当のソースコード
12
- ```
12
+ ```
13
13
  <!-- input.php -->
14
14
  <!DOCTYPE html>
15
15
  <html>
@@ -54,13 +54,62 @@
54
54
  </body>
55
55
  </html>
56
56
 
57
- ```
57
+ ```
58
-
58
+ 修正前
59
+ ```
59
60
  <!-- input_do.php -->
61
+
60
62
  <?php
61
63
 
62
64
  try{
63
65
  $pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8','root','suzusaga');
66
+ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
67
+ $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
68
+
69
+ $stmt = $pdo->prepare('INSERT INTO my_items (maker_id, item_name, price, keyword) VALUES (:maker_id, :item_name, :price, :keyword)');
70
+
71
+ $maker_id = htmlspecialchars($_POST["$d%"]);
72
+ $item_name = htmlspecialchars($_POST["%s%"]);
73
+ $price = htmlspecialchars($_POST["%d%"]);
74
+ $keyword = htmlspecialchars($_POST["%s"]);
75
+
76
+ $stmt->bindValue('maker_id', $maker_id);
77
+ $stmt->bindParam('item_name', $item_name);
78
+ $stmt->bindValue('price', $price);
79
+ $stmt->bindParam('keyword', $keyword);
80
+
81
+ $stmt->execute();
82
+
83
+ }
84
+
85
+ catch(PDOException $Exception) {
86
+ die('接続エラー:'.$Exception->getMessage());
87
+ }
88
+
89
+ ?>
90
+
91
+ <!DOCTYPE html>
92
+ <html>
93
+ <head>
94
+ <meta charset="utf-8">
95
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
96
+ <title></title>
97
+ <link rel="stylesheet" href="">
98
+ </head>
99
+ <body>
100
+ <p>登録完了しました。</p>
101
+ <p><a href="input.php">TOPに戻る</a></p>
102
+
103
+ </body>
104
+ </html>
105
+ ```
106
+ 修正後
107
+ ```ここに言語を入力
108
+ <!-- input_do.php -->
109
+ <?php
110
+
111
+ try{
112
+ $pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8','root','suzusaga');
64
113
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
65
114
  $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
66
115
  $sql = "SELECT * FROM my_items ORDER BY id DESC";
@@ -108,14 +157,18 @@
108
157
 
109
158
  </body>
110
159
  </html>
160
+ ```
161
+
162
+
111
163
 
112
- ```
113
164
 
165
+
114
166
 
115
167
 
116
168
 
117
169
 
118
170
 
171
+
119
172
  ###試したこと
120
173
 
121
174
 

3

文法の修正

2017/04/18 08:07

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -76,9 +76,9 @@
76
76
  $keyword = htmlspecialchars($_POST["keyword"]);
77
77
 
78
78
  $stmt->bindValue(':maker_id', $maker_id);
79
- $stmt->bindParam(':item_name', $item_name);
79
+ $stmt->bindValue(':item_name', $item_name);
80
80
  $stmt->bindValue(':price', $price);
81
- $stmt->bindParam(':keyword', $keyword);
81
+ $stmt->bindValue(':keyword', $keyword);
82
82
 
83
83
  $stmt->execute();
84
84
  var_dump($maker_id);

2

文法の修正

2017/04/18 07:55

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -63,21 +63,28 @@
63
63
  $pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8','root','suzusaga');
64
64
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
65
65
  $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
66
+ $sql = "SELECT * FROM my_items ORDER BY id DESC";
67
+ $stmt = $pdo->query($sql);
68
+
66
69
  print "接続に成功しました"."<br>";
67
70
 
68
71
  $stmt = $pdo->prepare('INSERT INTO my_items (maker_id, item_name, price, keyword) VALUES (:maker_id, :item_name, :price, :keyword)');
69
72
 
70
- $maker_id = htmlspecialchars($_POST["$d%"]);
73
+ $maker_id = htmlspecialchars($_POST["maker_id"]);
71
- $item_name = htmlspecialchars($_POST["%s%"]);
74
+ $item_name = htmlspecialchars($_POST["item_name"]);
72
- $price = htmlspecialchars($_POST["%d%"]);
75
+ $price = htmlspecialchars($_POST["price"]);
73
- $keyword = htmlspecialchars($_POST["%s"]);
76
+ $keyword = htmlspecialchars($_POST["keyword"]);
74
77
 
75
- $stmt->bindValue('maker_id', $maker_id);
78
+ $stmt->bindValue(':maker_id', $maker_id);
76
- $stmt->bindParam('item_name', $item_name);
79
+ $stmt->bindParam(':item_name', $item_name);
77
- $stmt->bindValue('price', $price);
80
+ $stmt->bindValue(':price', $price);
78
- $stmt->bindParam('keyword', $keyword);
81
+ $stmt->bindParam(':keyword', $keyword);
79
82
 
80
83
  $stmt->execute();
84
+ var_dump($maker_id);
85
+ var_dump($item_name);
86
+ var_dump($price);
87
+ var_dump($keyword);
81
88
 
82
89
  }
83
90
 

1

文法の修正

2017/04/18 07:53

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -9,6 +9,7 @@
9
9
 
10
10
 
11
11
  ###該当のソースコード
12
+ ```
12
13
  <!-- input.php -->
13
14
  <!DOCTYPE html>
14
15
  <html>
@@ -53,6 +54,8 @@
53
54
  </body>
54
55
  </html>
55
56
 
57
+ ```
58
+
56
59
  <!-- input_do.php -->
57
60
  <?php
58
61
 
@@ -60,6 +63,7 @@
60
63
  $pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8','root','suzusaga');
61
64
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
62
65
  $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
66
+ print "接続に成功しました"."<br>";
63
67
 
64
68
  $stmt = $pdo->prepare('INSERT INTO my_items (maker_id, item_name, price, keyword) VALUES (:maker_id, :item_name, :price, :keyword)');
65
69
 
@@ -98,13 +102,13 @@
98
102
  </body>
99
103
  </html>
100
104
 
105
+ ```
101
106
 
102
107
 
103
108
 
104
109
 
105
110
 
106
111
 
107
-
108
112
  ###試したこと
109
113
 
110
114