質問編集履歴
4
編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -75,4 +75,71 @@
|
|
75
75
|
</body>
|
76
76
|
</html>
|
77
77
|
|
78
|
-
です
|
78
|
+
です
|
79
|
+
|
80
|
+
pdo_form.php
|
81
|
+
|
82
|
+
<?php
|
83
|
+
header("Content-type: text/html; charset=utf-8");
|
84
|
+
|
85
|
+
if(empty($_POST)) {
|
86
|
+
header("Location: pdo_form.html");
|
87
|
+
exit();
|
88
|
+
}else{
|
89
|
+
//名前入力判定
|
90
|
+
if (!isset($_POST['yourname']) || $_POST['yourname'] === "" ){
|
91
|
+
$errors['name'] = "名前が入力されていません。";
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
if(count($errors) === 0){
|
96
|
+
|
97
|
+
$dsn = 'mysql:host=localhost;dbname=test5;charset=utf8';
|
98
|
+
$user = 'root';
|
99
|
+
$password = '10veikA11#';
|
100
|
+
|
101
|
+
try{
|
102
|
+
$dbh = new PDO($dsn, $user, $password);
|
103
|
+
$statement = $dbh->prepare("INSERT INTO name (name) VALUES (:name)");
|
104
|
+
|
105
|
+
if($statement){
|
106
|
+
$yourname = $_POST['yourname'];
|
107
|
+
//プレースホルダへ実際の値を設定する
|
108
|
+
$statement->bindValue(':name', $yourname, PDO::PARAM_STR);
|
109
|
+
|
110
|
+
if(!$statement->execute()){
|
111
|
+
$errors['error'] = "登録失敗しました。";
|
112
|
+
}
|
113
|
+
|
114
|
+
//データベース接続切断
|
115
|
+
$dbh = null;
|
116
|
+
}
|
117
|
+
|
118
|
+
}catch (PDOException $e){
|
119
|
+
print('Error:'.$e->getMessage());
|
120
|
+
$errors['error'] = "データベース接続失敗しました。";
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
?>
|
125
|
+
|
126
|
+
<!DOCTYPE html>
|
127
|
+
<html>
|
128
|
+
<head>
|
129
|
+
<title>登録画面</title>
|
130
|
+
<meta charset="utf-8">
|
131
|
+
</head>
|
132
|
+
<body>
|
133
|
+
|
134
|
+
<?php if (count($errors) === 0): ?>
|
135
|
+
<p><?=htmlspecialchars($yourname, ENT_QUOTES, 'UTF-8')."さんで登録いたしました。"?></p>
|
136
|
+
<?php elseif(count($errors) > 0): ?>
|
137
|
+
<?php
|
138
|
+
foreach($errors as $value){
|
139
|
+
echo "<p>".$value."</p>";
|
140
|
+
}
|
141
|
+
?>
|
142
|
+
<?php endif; ?>
|
143
|
+
|
144
|
+
</body>
|
145
|
+
</html>
|
3
編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -27,7 +27,7 @@
|
|
27
27
|
|
28
28
|
$dsn = 'mysql:host=localhost;dbname=test5;charset=utf8';
|
29
29
|
$user = 'root';
|
30
|
-
$password = '
|
30
|
+
$password = 'root';
|
31
31
|
|
32
32
|
try{
|
33
33
|
$dbh = new PDO($dsn, $user, $password);
|
2
編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,4 +6,73 @@
|
|
6
6
|
|
7
7
|
下のようにコードがそのまま表示されます
|
8
8
|
|
9
|
-

|
9
|
+

|
10
|
+
|
11
|
+
pdo_form.html
|
12
|
+
|
13
|
+
<?php
|
14
|
+
header("Content-type: text/html; charset=utf-8");
|
15
|
+
|
16
|
+
if(empty($_POST)) {
|
17
|
+
header("Location: pdo_form.html");
|
18
|
+
exit();
|
19
|
+
}else{
|
20
|
+
//名前入力判定
|
21
|
+
if (!isset($_POST['yourname']) || $_POST['yourname'] === "" ){
|
22
|
+
$errors['name'] = "名前が入力されていません。";
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
if(count($errors) === 0){
|
27
|
+
|
28
|
+
$dsn = 'mysql:host=localhost;dbname=test5;charset=utf8';
|
29
|
+
$user = 'root';
|
30
|
+
$password = '10veikA11#';
|
31
|
+
|
32
|
+
try{
|
33
|
+
$dbh = new PDO($dsn, $user, $password);
|
34
|
+
$statement = $dbh->prepare("INSERT INTO name (name) VALUES (:name)");
|
35
|
+
|
36
|
+
if($statement){
|
37
|
+
$yourname = $_POST['yourname'];
|
38
|
+
//プレースホルダへ実際の値を設定する
|
39
|
+
$statement->bindValue(':name', $yourname, PDO::PARAM_STR);
|
40
|
+
|
41
|
+
if(!$statement->execute()){
|
42
|
+
$errors['error'] = "登録失敗しました。";
|
43
|
+
}
|
44
|
+
|
45
|
+
//データベース接続切断
|
46
|
+
$dbh = null;
|
47
|
+
}
|
48
|
+
|
49
|
+
}catch (PDOException $e){
|
50
|
+
print('Error:'.$e->getMessage());
|
51
|
+
$errors['error'] = "データベース接続失敗しました。";
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
?>
|
56
|
+
|
57
|
+
<!DOCTYPE html>
|
58
|
+
<html>
|
59
|
+
<head>
|
60
|
+
<title>登録画面</title>
|
61
|
+
<meta charset="utf-8">
|
62
|
+
</head>
|
63
|
+
<body>
|
64
|
+
|
65
|
+
<?php if (count($errors) === 0): ?>
|
66
|
+
<p><?=htmlspecialchars($yourname, ENT_QUOTES, 'UTF-8')."さんで登録いたしました。"?></p>
|
67
|
+
<?php elseif(count($errors) > 0): ?>
|
68
|
+
<?php
|
69
|
+
foreach($errors as $value){
|
70
|
+
echo "<p>".$value."</p>";
|
71
|
+
}
|
72
|
+
?>
|
73
|
+
<?php endif; ?>
|
74
|
+
|
75
|
+
</body>
|
76
|
+
</html>
|
77
|
+
|
78
|
+
です
|
1
画像追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,4 +2,8 @@
|
|
2
2
|
|
3
3
|
/Applications/XAMPP/htdocs/ でしょうか?
|
4
4
|
|
5
|
-
それとも どこでもいいのでしょうか?
|
5
|
+
それとも どこでもいいのでしょうか?
|
6
|
+
|
7
|
+
下のようにコードがそのまま表示されます
|
8
|
+
|
9
|
+

|