質問編集履歴
2
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,17 +4,10 @@
|
|
4
4
|
情報入力後、送信ボタンを押しても作成したテーブルに登録されません。
|
5
5
|
エラーメッセージ等は出ずデータベースにも接続はできている状態です。
|
6
6
|
登録処理はcontact.phpのHTML上部に記載しています。
|
7
|
-
INSERT INTO contacts()内のカラム名は間違っていません。
|
8
7
|
入力画面:contact.php
|
9
8
|
確認画面:confirm.php
|
10
|
-
-質問-
|
11
|
-
・VALUES()中のパラメータ名は任意でつけていいのは間違っていませんでしょうか?
|
12
|
-
・登録処理を記載する場所はcontact.phpでいいのでしょうか?
|
13
9
|
当方、PHP,SQL勉強初めて一か月の初学者です。
|
14
10
|
知識はまだ足りないことばかりなのでアドバイスいただけると幸いです。
|
15
|
-
### 実現したいこと
|
16
|
-
お問い合わせフォームで入力された情報をデータベースに登録したい。
|
17
|
-
|
18
11
|
### 発生している問題・エラーメッセージ
|
19
12
|
|
20
13
|
```
|
@@ -22,13 +15,18 @@
|
|
22
15
|
```
|
23
16
|
|
24
17
|
### 該当のソースコード
|
25
|
-
|
18
|
+
```DBの情報
|
19
|
+
テーブル名 contacts
|
20
|
+
カラム名: id name kana tel email body created_at
|
21
|
+
パラメータ名:システムID 氏名 フリガナ 電話番号 メールアドレス お問い合わせ内容 送信日時
|
22
|
+
|
23
|
+
```
|
26
|
-
```
|
24
|
+
```contact.php
|
27
25
|
<?php
|
28
|
-
|
26
|
+
|
29
27
|
function db_open() :PDO{
|
30
28
|
$user = "root";
|
31
|
-
$password = "
|
29
|
+
$password = "sample";
|
32
30
|
$opt = [
|
33
31
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
34
32
|
PDO::ATTR_EMULATE_PREPARES => false,
|
@@ -58,11 +56,11 @@
|
|
58
56
|
)
|
59
57
|
';
|
60
58
|
$stmt = $dbh->prepare($sql);
|
61
|
-
$stmt->bind
|
59
|
+
$stmt->bindValue(':FULLNAME', $_POST['name'], PDO::PARAM_STR);
|
62
|
-
$stmt->bind
|
60
|
+
$stmt->bindValue(':KATAKANA', $_POST['kana'], PDO::PARAM_STR);
|
63
|
-
$stmt->bind
|
61
|
+
$stmt->bindValue(':TEL', $_POST['num'], PDO::PARAM_INT);
|
64
|
-
$stmt->bind
|
62
|
+
$stmt->bindValue(':EMAIL', $_POST['mail'], PDO::PARAM_STR);
|
65
|
-
$stmt->bind
|
63
|
+
$stmt->bindValue(':TEXTAREA', $_POST['message'], PDO::PARAM_STR);
|
66
64
|
$stmt->execute();
|
67
65
|
|
68
66
|
/* ↓一つ前のページのパスを指定し、処理が終わったらそこに戻る */
|
@@ -73,12 +71,34 @@
|
|
73
71
|
}
|
74
72
|
?>
|
75
73
|
|
74
|
+
<!DOCTYPE html>
|
75
|
+
<html lang="ja">
|
76
|
+
|
77
|
+
<head>
|
78
|
+
<meta charset="UTF-8">
|
79
|
+
<title>Sample</title>
|
80
|
+
<link rel="stylesheet" type="text/css" href="form.css">
|
81
|
+
<link rel="stylesheet" type="text/css" href="formHeader.css">
|
82
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
83
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
|
84
|
+
</head>
|
85
|
+
|
86
|
+
<body>
|
87
|
+
<main>
|
88
|
+
<?php include('./header.php'); ?>
|
76
|
-
|
89
|
+
<div id="contact">
|
90
|
+
<h1 id="contact_h1">お問い合わせ</h1>
|
91
|
+
<div id="table">
|
92
|
+
<p class="p_gray">下記の項目をご記入の上送信ボタンを押してください</p>
|
93
|
+
<p>送信頂いた件につきましては、当社より折り返しご連絡を差し上げます。</p>
|
94
|
+
<p>なお、ご連絡までに、お時間を頂く場合もございますので予めご了承ください。</p>
|
95
|
+
<p><span>*</span>は必須項目となります。</p>
|
96
|
+
<div id="contact_form">
|
77
|
-
<form action="confirm.php" method="post">
|
97
|
+
<form action="confirm.php" method="post">
|
78
98
|
<p>氏名<span>*</span></p>
|
79
|
-
<input type="text" id="name" name="name" placeholder="
|
99
|
+
<input type="text" id="name" name="name" placeholder="山田太郎">
|
80
100
|
<p>フリガナ<span>*</span></p>
|
81
|
-
<input type="text" id="kana" name="kana" placeholder="
|
101
|
+
<input type="text" id="kana" name="kana" placeholder="ヤマダタロウ">
|
82
102
|
<p>電話番号</p>
|
83
103
|
<input type="text" id="num" name="num" placeholder="09012345678">
|
84
104
|
<p>メールアドレス<span>*</span></p>
|
@@ -87,11 +107,107 @@
|
|
87
107
|
<textarea id="message" name="message"></textarea>
|
88
108
|
<input type="submit" class="submit" id="submit" value="送信">
|
89
109
|
</form>
|
110
|
+
</section>
|
111
|
+
<!-- DBからデータの取得と表示 -->
|
112
|
+
|
113
|
+
<?php
|
114
|
+
try{
|
115
|
+
$dbh = db_open();
|
116
|
+
$sql = 'SELECT id, name, kana, tel, email, body, created_at FROM contacts';
|
117
|
+
$statement = $dbh -> query($sql);
|
118
|
+
?>
|
119
|
+
<section class="DB">
|
120
|
+
<table>
|
121
|
+
<tr>
|
122
|
+
<th>システムID</th>
|
123
|
+
<th>氏名</th>
|
124
|
+
<th>フリガナ</th>
|
125
|
+
<th>電話番号</th>
|
126
|
+
<th>メールアドレス</th>
|
127
|
+
<th>お問い合わせ内容</th>
|
128
|
+
<th>送信日時</th>
|
129
|
+
</tr>
|
130
|
+
<?php while($row = $statement -> fetch()): ?>
|
131
|
+
|
132
|
+
<tr>
|
133
|
+
<th><?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8')?></th>
|
134
|
+
<th><?php echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8')?></th>
|
135
|
+
<th><?php echo htmlspecialchars($row['kana'], ENT_QUOTES, 'UTF-8')?></th>
|
136
|
+
<th><?php echo htmlspecialchars($row['tel'], ENT_QUOTES, 'UTF-8')?></th>
|
137
|
+
<th><?php echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8')?></th>
|
138
|
+
<th><?php echo htmlspecialchars($row['body'], ENT_QUOTES, 'UTF-8')?></th>
|
139
|
+
<th><?php echo htmlspecialchars($row['created_at'], ENT_QUOTES, 'UTF-8')?></th>
|
140
|
+
<th><a href="edit.php?id=<?php echo (int) $row['id'];?>">編集</a></th>
|
141
|
+
<th><a href="delete.php?id=<?php echo (int) $row['id'];?>">削除</a></th>
|
142
|
+
|
143
|
+
</tr>
|
144
|
+
<?php endwhile;?>
|
145
|
+
</table>
|
146
|
+
<?php
|
147
|
+
} catch(PDOException $e){
|
148
|
+
echo '接続失敗'. $e-> getMessage();
|
149
|
+
exit();
|
150
|
+
}
|
151
|
+
?>
|
152
|
+
</section>
|
153
|
+
</div>
|
154
|
+
</div>
|
155
|
+
</div>
|
156
|
+
<?php include('./footer.php'); ?>
|
157
|
+
</main>
|
158
|
+
</body>
|
159
|
+
<script>
|
160
|
+
window.onload = function() {
|
161
|
+
const submit = document.getElementById('submit');
|
162
|
+
const name = document.getElementById('name');
|
163
|
+
const kana = document.getElementById('kana');
|
164
|
+
const num = document.getElementById('num');
|
165
|
+
const mail = document.getElementById('mail');
|
166
|
+
const message = document.getElementById('message');
|
167
|
+
const mailSt = /^[A-Za-z0-9]{1}[A-Za-z0-9_.-]*@{1}[A-Za-z0-9_.-]{1,}.[A-Za-z0-9]{1,}$/;
|
168
|
+
const numSt = /^\d*$/;
|
169
|
+
const n = name.value;
|
170
|
+
console.log(submit);
|
171
|
+
console.log(n);
|
172
|
+
|
173
|
+
submit.addEventListener('click', function(event) {
|
174
|
+
let error = [];
|
175
|
+
if (name.value === "") {
|
176
|
+
error.push("氏名は必須入力です。\n");
|
177
|
+
}
|
178
|
+
if (name.value.length > 10) {
|
179
|
+
error.push("氏名は10文字以内で入力してください。\n");
|
180
|
+
}
|
181
|
+
if (kana.value === "") {
|
182
|
+
error.push("フリガナは必須入力です。\n");
|
183
|
+
}
|
184
|
+
if (kana.value.length > 10) {
|
185
|
+
error.push("フリガナは10文字以内で入力してください。\n");
|
186
|
+
}
|
187
|
+
if (!numSt.test(num.value)) {
|
188
|
+
error.push("半角数字で入力してください。\n");
|
189
|
+
}
|
190
|
+
if (mail.value === "") {
|
191
|
+
error.push("メールアドレスは必須入力です。\n");
|
192
|
+
}
|
193
|
+
if ((!mailSt.test(mail.value)) && (mail.value.length > 0)) {
|
194
|
+
error.push("メールアドレスの形式が不正です。\n");
|
195
|
+
}
|
196
|
+
if (message.value === "") {
|
197
|
+
error.push("お問い合わせ内容は必須入力です。\n");
|
198
|
+
}
|
199
|
+
if (error.length > 0) {
|
200
|
+
alert(error);
|
201
|
+
return;
|
202
|
+
}
|
203
|
+
});
|
204
|
+
};
|
205
|
+
</script>
|
206
|
+
|
207
|
+
</html>
|
90
208
|
```
|
91
209
|
|
92
210
|
### 試したこと
|
93
|
-
|
94
|
-
|
95
211
|
値の受け渡しができていないので登録されていないと思っています、insert文を使用しPOSTで送られてきた情報のキーから受け渡しを図ったが反応しません、、、
|
96
212
|
### 補足情報(FW/ツールのバージョンなど)
|
97
213
|
xamppを使用しlocalhostで表示
|
1
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -92,7 +92,7 @@
|
|
92
92
|
### 試したこと
|
93
93
|
|
94
94
|
|
95
|
-
値の受け渡しができていないので登録されていないと思
|
95
|
+
値の受け渡しができていないので登録されていないと思っています、insert文を使用しPOSTで送られてきた情報のキーから受け渡しを図ったが反応しません、、、
|
96
96
|
### 補足情報(FW/ツールのバージョンなど)
|
97
97
|
xamppを使用しlocalhostで表示
|
98
98
|
phpのバージョンは7.4.28
|