質問編集履歴
3
更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -126,4 +126,8 @@
|
|
126
126
|
|
127
127
|
</body>
|
128
128
|
|
129
|
-
</html>
|
129
|
+
</html>
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
![イメージ説明](58e31f9a5acee49a0eb2d2c984daabe9.jpeg)
|
2
更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -42,7 +42,7 @@
|
|
42
42
|
|
43
43
|
try{
|
44
44
|
|
45
|
-
$dbh = new PDO( 'localhost', 'root2', '
|
45
|
+
$dbh = new PDO( 'localhost', 'root2', 'pass', 'test' );
|
46
46
|
|
47
47
|
$statement = $dbh->prepare("INSERT INTO test2 (name) VALUES (:name)");
|
48
48
|
|
1
更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -1 +1,129 @@
|
|
1
1
|
mysql に php でinsertするとき 画面が白くなるだけでinsertされないのはなぜでしょう ?
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
<?php
|
6
|
+
|
7
|
+
header("Content-type: text/html; charset=utf-8");
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
if(empty($_POST)) {
|
12
|
+
|
13
|
+
header("Location: form1.html");
|
14
|
+
|
15
|
+
exit();
|
16
|
+
|
17
|
+
}else{
|
18
|
+
|
19
|
+
//名前入力判定
|
20
|
+
|
21
|
+
if (!isset($_POST['yourname']) || $_POST['yourname'] === "" ){
|
22
|
+
|
23
|
+
$errors['name'] = "名前が入力されていません。";
|
24
|
+
|
25
|
+
}
|
26
|
+
|
27
|
+
}
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
if(count($errors) === 0){
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
$dsn = 'mysql:host=localhost;dbname=test;charset=utf8';
|
36
|
+
|
37
|
+
$user = 'root2';
|
38
|
+
|
39
|
+
$password = 'pass';
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
try{
|
44
|
+
|
45
|
+
$dbh = new PDO( 'localhost', 'root2', '10veikA11#', 'test' );
|
46
|
+
|
47
|
+
$statement = $dbh->prepare("INSERT INTO test2 (name) VALUES (:name)");
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
if($statement){
|
52
|
+
|
53
|
+
$yourname = $_POST['yourname'];
|
54
|
+
|
55
|
+
//プレースホルダへ実際の値を設定する
|
56
|
+
|
57
|
+
$statement->bindValue(':name', $yourname, PDO::PARAM_STR);
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
if(!$statement->execute()){
|
62
|
+
|
63
|
+
$errors['error'] = "登録失敗しました。";
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
$dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
}catch (PDOException $e){
|
76
|
+
|
77
|
+
print('Error:'.$e->getMessage());
|
78
|
+
|
79
|
+
$errors['error'] = "データベース接続失敗しました。";
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
?>
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
<!DOCTYPE html>
|
92
|
+
|
93
|
+
<html>
|
94
|
+
|
95
|
+
<head>
|
96
|
+
|
97
|
+
<title>登録画面</title>
|
98
|
+
|
99
|
+
<meta charset="utf-8">
|
100
|
+
|
101
|
+
</head>
|
102
|
+
|
103
|
+
<body>
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
<?php if (count($errors) === 0): ?>
|
108
|
+
|
109
|
+
<p><?=htmlspecialchars($yourname, ENT_QUOTES, 'UTF-8')."さんで登録いたしました。"?></p>
|
110
|
+
|
111
|
+
<?php elseif(count($errors) > 0): ?>
|
112
|
+
|
113
|
+
<?php
|
114
|
+
|
115
|
+
foreach($errors as $value){
|
116
|
+
|
117
|
+
echo "<p>".$value."</p>";
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
?>
|
122
|
+
|
123
|
+
<?php endif; ?>
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
</body>
|
128
|
+
|
129
|
+
</html>
|