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

質問編集履歴

2

ソースに間違い

2021/12/08 05:04

投稿

pofinpouty
pofinpouty

スコア20

title CHANGED
File without changes
body CHANGED
@@ -72,8 +72,8 @@
72
72
 
73
73
  $stmt = $dbh->prepare('insert into Test(beforeFile, afterFile) value (?, ?)');
74
74
 
75
+ $stmt->bindValue(1, $before);
75
- $stmt->bindValue(1, $marker);
76
+ $stmt->bindValue(2, $after);
76
- $stmt->bindValue(2, $contents);
77
77
  $stmt->execute();
78
78
 
79
79
  $dbh = null;

1

回答いただいた内容の反映

2021/12/08 05:04

投稿

pofinpouty
pofinpouty

スコア20

title CHANGED
File without changes
body CHANGED
@@ -48,4 +48,58 @@
48
48
  ```
49
49
 
50
50
  固定の値でボタンをクリックすると、登録されたのを確認できました。
51
- どこが原因なのか教えていただきたいです。
51
+ どこが原因なのか教えていただきたいです。
52
+
53
+
54
+ **追記**
55
+ ご回答いただいた内容を反映しました。
56
+ ボタンクリック時にデータベースに1行追加されていることを確認できたのですが、自動生成される
57
+ IDのカラム以外は空白で登録されておりました。
58
+ INSERT文で"beforeFile"と"afterFile"を指定しているので間違っていないと思っていたのですが。。。
59
+ 原因がありましたら教えていただきたいです。
60
+ ```PHP
61
+ <?php
62
+
63
+ $dsn = 'mysql:host=*****;dbname=Test;charset=utf8;unix_socket=/mysql.sock';
64
+ $user = 'user';
65
+ $password = 'pass';
66
+
67
+ if (!empty($_POST)) {
68
+ $dbh = new PDO($dsn, $user, $password);
69
+
70
+ $before = isset($_POST['beforeFile']) ? $_POST['beforeFile'] : '';
71
+ $after = isset($_POST['afterFile']) ? $_POST['afterFile'] : '';
72
+
73
+ $stmt = $dbh->prepare('insert into Test(beforeFile, afterFile) value (?, ?)');
74
+
75
+ $stmt->bindValue(1, $marker);
76
+ $stmt->bindValue(2, $contents);
77
+ $stmt->execute();
78
+
79
+ $dbh = null;
80
+ //メッセージのセット
81
+ $msg = '登録完了';
82
+ }
83
+ ?>
84
+
85
+ <head>
86
+ <title>登録画面</title>
87
+ <meta http-equiv="content-type" charset="UTF-8">
88
+ </head>
89
+
90
+ <html>
91
+
92
+ <form action="insert.php" method="post">
93
+ <p>before</p>
94
+ <input type="file" name="beforeFile" accept="image/jpg, image/png" required>
95
+ <p>after</p>
96
+ <input type="file" name="afterFile" accept="image/jpg" readonly>
97
+
98
+ <p><input type="submit" value="登録"></p>
99
+ </form>
100
+
101
+ <?php if ($msg): ?>
102
+ <p><?=$msg?></p>
103
+ <?php endif; ?>
104
+ </html>
105
+ ```