質問編集履歴

1

修正したソースを追記

2015/11/25 05:53

投稿

junkboy
junkboy

スコア45

test CHANGED
File without changes
test CHANGED
@@ -81,3 +81,83 @@
81
81
  }
82
82
 
83
83
  ```
84
+
85
+
86
+
87
+ ↓Kosuke_Shibuyaさん、KiyoshiMotokiさんにご指摘いただいた内容を加味し、ソースを修正しました。
88
+
89
+
90
+
91
+ ```lang-php
92
+
93
+ function cuteInsert() {
94
+
95
+ $dbh = connectDb();
96
+
97
+ // $post_id = h($_POST['post_id']); から変更
98
+
99
+ $post_id = filter_input(INPUT_POST, 'post_id');
100
+
101
+
102
+
103
+ $sql = "insert ignore into post_ip(post_id, remote_addr, user_agent, created ) values (:post_id, :remote_addr, :user_agent, now())";
104
+
105
+ $dbh->beginTransaction();
106
+
107
+ $stmt = $dbh->prepare($sql);
108
+
109
+ $stmt->bindValue(':post_id', $post_id, PDO::PARAM_INT);
110
+
111
+ $stmt->bindValue(':remote_addr', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
112
+
113
+ $stmt->bindValue(':user_agent', $_SERVER['HTTP_USER_AGENT'], PDO::PARAM_STR);
114
+
115
+ $stmt->execute();
116
+
117
+ $count = $stmt->rowCount();
118
+
119
+
120
+
121
+ // 登録できた場合
122
+
123
+ if ($count == "1") {
124
+
125
+ $eva = filter_input(INPUT_POST, 'evaluation');
126
+
127
+ if ($eva == "0") {
128
+
129
+ $evaluation = 1;
130
+
131
+ } else {
132
+
133
+ $evaluation = $eva + 1;
134
+
135
+ }
136
+
137
+
138
+
139
+ $cute_sql = "update post set evaluation = :evaluation where post_id = :post_id";
140
+
141
+
142
+
143
+ $stmt = $dbh->prepare($cute_sql);
144
+
145
+ $stmt->bindValue(':post_id', $post_id, PDO::PARAM_INT);
146
+
147
+ $stmt->bindValue(':evaluation', $evaluation, PDO::PARAM_INT);
148
+
149
+ $stmt->execute();
150
+
151
+ $dbh->commit();
152
+
153
+ // 登録できなかった場合
154
+
155
+ } else {
156
+
157
+ $dbh->rollBack();
158
+
159
+ }
160
+
161
+ }
162
+
163
+ ```