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

質問編集履歴

1

修正したソースを追記

2015/11/25 05:53

投稿

junkboy
junkboy

スコア45

title CHANGED
File without changes
body CHANGED
@@ -39,4 +39,44 @@
39
39
 
40
40
  return $evaluation;
41
41
  }
42
+ ```
43
+
44
+ ↓Kosuke_Shibuyaさん、KiyoshiMotokiさんにご指摘いただいた内容を加味し、ソースを修正しました。
45
+
46
+ ```lang-php
47
+ function cuteInsert() {
48
+ $dbh = connectDb();
49
+ // $post_id = h($_POST['post_id']); から変更
50
+ $post_id = filter_input(INPUT_POST, 'post_id');
51
+
52
+ $sql = "insert ignore into post_ip(post_id, remote_addr, user_agent, created ) values (:post_id, :remote_addr, :user_agent, now())";
53
+ $dbh->beginTransaction();
54
+ $stmt = $dbh->prepare($sql);
55
+ $stmt->bindValue(':post_id', $post_id, PDO::PARAM_INT);
56
+ $stmt->bindValue(':remote_addr', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
57
+ $stmt->bindValue(':user_agent', $_SERVER['HTTP_USER_AGENT'], PDO::PARAM_STR);
58
+ $stmt->execute();
59
+ $count = $stmt->rowCount();
60
+
61
+ // 登録できた場合
62
+ if ($count == "1") {
63
+ $eva = filter_input(INPUT_POST, 'evaluation');
64
+ if ($eva == "0") {
65
+ $evaluation = 1;
66
+ } else {
67
+ $evaluation = $eva + 1;
68
+ }
69
+
70
+ $cute_sql = "update post set evaluation = :evaluation where post_id = :post_id";
71
+
72
+ $stmt = $dbh->prepare($cute_sql);
73
+ $stmt->bindValue(':post_id', $post_id, PDO::PARAM_INT);
74
+ $stmt->bindValue(':evaluation', $evaluation, PDO::PARAM_INT);
75
+ $stmt->execute();
76
+ $dbh->commit();
77
+ // 登録できなかった場合
78
+ } else {
79
+ $dbh->rollBack();
80
+ }
81
+ }
42
82
  ```