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

回答編集履歴

1

sample

2019/06/03 03:49

投稿

yambejp
yambejp

スコア117923

answer CHANGED
@@ -2,4 +2,69 @@
2
2
  ユーザーから受け取った文字列を利用してカラムを埋め込むのはNGです
3
3
  (管理画面や自分専用を除く)
4
4
  また中身が空文字""のときにNULLと比較してしまうのでまったくだめです
5
- そもそもNULLを検出する演算子は等号ではなくisです
5
+ そもそもNULLを検出する演算子は等号ではなくisです
6
+
7
+ # sample
8
+ ちゃんとやるなら最低限こうしてください
9
+ ```PHP
10
+ <?PHP
11
+ $shift_flag=filter_input(INPUT_POST,"shift_flag");
12
+ $is_null=filter_input(INPUT_POST,"is_null",FILTER_DEFAULT,FILTER_REQUIRE_ARRAY)?:[];
13
+ $set="";
14
+ $data=[];
15
+ if(in_array("shift_flag",$is_null)){
16
+ $set.=",shift_flag = null";
17
+ }elseif(!is_null($shift_flag)){
18
+ $set.=",shift_flag = ?";
19
+ $data[]=$shift_flag;
20
+ }
21
+ print $set."<br>";
22
+ print_r($data);
23
+ ?>
24
+ <script>
25
+ window.addEventListener('DOMContentLoaded', function(e){
26
+ [].forEach.call(document.querySelectorAll('[name="is_null[]"]'),function(x){
27
+ x.addEventListener('change',function(e){
28
+ document.querySelector('[name="'+e.target.value+'"]').disabled=e.target.checked;
29
+ });
30
+ });
31
+ });
32
+ </script>
33
+ <form method="post">
34
+ シフトフラグ:<input type="text" name="shift_flag" value="0" id="shift_flag">
35
+ <label><input type="checkbox" name="is_null[]" value="shift_flag">null</label><br>
36
+ <input type="submit" value="send"></form>
37
+ ```
38
+ さらにバリデートを厳密に適用するならこう
39
+ ```PHP
40
+ <?PHP
41
+ $shift_flag=filter_input(INPUT_POST,"shift_flag",FILTER_VALIDATE_INT);
42
+ $is_null=filter_input(INPUT_POST,"is_null",FILTER_DEFAULT,FILTER_REQUIRE_ARRAY)?:[];
43
+ $set="";
44
+ $data=[];
45
+
46
+ if(in_array("shift_flag",$is_null)){
47
+ $set.=",shift_flag = null";
48
+ }elseif($shift_flag===false){
49
+ print "error";
50
+ }elseif(!is_null($shift_flag)){
51
+ $set.=",shift_flag = ?";
52
+ $data[]=$shift_flag;
53
+ }
54
+ print $set."<br>";
55
+ print_r($data);
56
+ ?>
57
+ <script>
58
+ window.addEventListener('DOMContentLoaded', function(e){
59
+ [].forEach.call(document.querySelectorAll('[name="is_null[]"]'),function(x){
60
+ x.addEventListener('change',function(e){
61
+ document.querySelector('[name="'+e.target.value+'"]').disabled=e.target.checked;
62
+ });
63
+ });
64
+ });
65
+ </script>
66
+ <form method="post">
67
+ シフトフラグ:<input type="text" name="shift_flag" value="0" id="shift_flag" pattern="\d+" required>
68
+ <label><input type="checkbox" name="is_null[]" value="shift_flag">null</label><br>
69
+ <input type="submit" value="send"></form>
70
+ ```