質問編集履歴

2

$dbのreturnを削除し、以下を可変長プレースホルダの形に書き換えました。また、HTMLのhtmlspecialchars部分をphp内に記述してみました。

2019/11/26 15:32

投稿

TaraIkura
TaraIkura

スコア5

test CHANGED
File without changes
test CHANGED
@@ -170,91 +170,73 @@
170
170
 
171
171
  function getUserData() {
172
172
 
173
- //DBの接続情報
173
+
174
174
 
175
175
  include_once( 'database.php' );
176
176
 
177
177
 
178
178
 
179
- //DBコネクタを生成
180
-
181
- try {
179
+ try {
182
180
 
183
181
  $db = new PDO( $dsn, $usr, $passwd );
184
182
 
185
- $db->exec( 'SET NAMES utf8' );
186
-
187
183
  $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
188
184
 
189
185
  $db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
190
186
 
187
+
188
+
189
+ $q = ( string )filter_input( INPUT_GET, 'q' );
190
+
191
+ $maxKeywords = -1;
192
+
193
+ $keywords = preg_split( '/(?:\p{Z}|\p{Cc})++/u', $q, $maxKeywords, PREG_SPLIT_NO_EMPTY );
194
+
195
+
196
+
197
+ if ( $keywords ) {
198
+
199
+ foreach ( $keywords as $keyword ) {
200
+
201
+ $holders[] = "((search_name LIKE ? ESCAPE '!') OR (color LIKE ? ESCAPE '!') OR (category LIKE ? ESCAPE '!'))";
202
+
203
+ $values[] = $values[] = '%' . preg_replace( '/(?=[!_%])/', '!', $keyword ) . '%';
204
+
205
+ }
206
+
207
+
208
+
209
+ $sql = "SELECT * FROM TABLE1 WHERE (' . implode(' AND ', $holders) . ')";
210
+
211
+
212
+
213
+ $UserDataSet = $bd->prepare( $sql );
214
+
215
+ $UserDataSet->execute( $values );
216
+
217
+ $userData = $UserDataSet->fetchAll( PDO::FETCH_ASSOC );
218
+
219
+ }
220
+
221
+
222
+
223
+ print '接続に成功しました';
224
+
225
+
226
+
191
227
  } catch ( PDOException $e ) {
192
228
 
229
+ header( 'Content-Type: text/plain; charset=UTF-8', true, 500 );
230
+
193
- die( "接続エラー1: {$e->getMessage()}" );
231
+ exit( '接続できませんでした' . $e->getMessage() );
194
-
232
+
195
- }
233
+ }
196
-
197
- return $db;
234
+
198
-
199
-  //ここまでは接続確認済です。
235
+
200
-
201
-
202
-
236
+
203
- //queryのデータ
237
+ function h( $str ) {
204
-
205
- try {
238
+
206
-
207
-   $query = "SELECT * FROM TABLE1 WHERE search_name LIKE ? ESCAPE '!' AND color LIKE ? ESCAPE '!' AND category LIKE ? ESCAPE '!' ";
208
-
209
- $qPart = array_fill( 0, count( $userData ), "(?, ?, ?, ?, ?)" );
239
+ return htmlspecialchars( $str, ENT_QUOTES, 'UTF-8' );
210
-
211
- $query .= implode( ",", $qPart );
212
-
213
-
214
-
215
- $search_name = $_POST[ 'search_name' ];
216
-
217
- $color = $_POST[ 'color' ];
218
-
219
- $category = $_POST[ 'category' ];
220
-
221
-
222
-
223
- //SQL文を実行する
224
-
225
- $UserDataSet = $db->prepare( $query );
226
-
227
- $i = 1;
228
-
229
- foreach ( $userData as $row ) { //bind the values one by one
230
-
231
- $stmt->bindValue( $i++, $row[ '%' . preg_replace( '/(?=[!_%])/', '!', $search_name ) . '%' ], PDO::PARAM_STR );
232
-
233
- $stmt->bindValue( $i++, $row[ '%' . preg_replace( '/(?=[!_%])/', '!', $color ) . '%' ], PDO::PARAM_STR );
234
-
235
- $stmt->bindValue( $i++, $row[ '%' . preg_replace( '/(?=[!_%])/', '!', $category ) . '%' ], PDO::PARAM_STR );
236
-
237
- }
238
-
239
- $UserDataSet->execute();
240
-
241
- //扱いやすい形に変える
242
-
243
- $result = [];
244
-
245
- while ( $row = $UserDataSet->fetch(PDO::FETCH_ASSOC)) {
246
-
247
- $result[] = $row;
248
-
249
- }
250
-
251
- return $result;
252
-
253
-
254
-
255
- } catch ( PDOException $e ) {
256
-
257
- die( "接続エラー2: {$e->getMessage()}" );
258
240
 
259
241
  }
260
242
 
@@ -270,7 +252,7 @@
270
252
 
271
253
  include_once( 'search.php' );
272
254
 
273
- $userData = getUserData( $_POST );
255
+ $userData = getUserData( $_GET );
274
256
 
275
257
  ?>
276
258
 
@@ -284,7 +266,7 @@
284
266
 
285
267
    //キーワード検索、チェックボックス、セレクションの3タイプで検索
286
268
 
287
-   <input name="search_name" value="<?php echo isset($search_name) ? htmlspecialchars($search_name) : '' ?>">
269
+   <input name="search_name" value="<?php echo isset($search_name) ?= h($search_name) : '' ?>">
288
270
 
289
271
  <input type="checkbox" name="color" value="白" <?php echo isset($color) && $color == '白' ? 'selected' : '' ?>><label for="color">白</label>//他省略
290
272
 
@@ -298,19 +280,29 @@
298
280
 
299
281
    
300
282
 
301
- //上記項目以外のデータで結果表示
283
+   //$holders[]に指定した列以外のデータで結果表示
302
-
284
+
303
- <?php if(isset($userData)): ?>
285
+ <?php if (isset($err)) : ?>
286
+
304
-
287
+ <p><?= h($err->getMessage()); ?></p>
288
+
289
+ <?php endif; ?>
290
+
291
+ <?php if (0 < count($userData)): ?>
292
+
305
- <?php foreach($userData as $row): ?>
293
+ <?php foreach ($userData as $row) : ?>
294
+
306
-
295
+ <div class="box_warp">
296
+
297
+ <div><img class="circular" height="80" src="../<?= h($row['img']); ?>"></div>
298
+
307
- <img src="../<?php echo htmlspecialchars($row['img']); ?>">
299
+ <div class="break-word"><?= h($row['original_name']); ?></div>
308
-
300
+
309
- <?php echo htmlspecialchars($row['original_name']) ?>
301
+ </div>
310
-
302
+
311
- <?php endforeach; ?>
303
+ <?php endforeach; ?>
312
-
304
+
313
- <?php endif; ?>
305
+ <?php endif; ?>
314
306
 
315
307
  </body>
316
308
 

1

1. 接続が確認できている部分にコメント追加 2. database.phpに設定した$usertable をテーブル名に変更 3. ->execute();の位置を変更

2019/11/26 15:32

投稿

TaraIkura
TaraIkura

スコア5

test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  $Mysqli->set_charset( 'utf8' );
32
32
 
33
- $query = "SELECT * FROM $usertable";
33
+ $query = "SELECT * FROM TABLE1";
34
34
 
35
35
  //接続状況チェック
36
36
 
@@ -42,8 +42,6 @@
42
42
 
43
43
  }
44
44
 
45
-
46
-
47
45
  //入力された検索条件からSQl文を生成
48
46
 
49
47
  $where = [];
@@ -70,11 +68,11 @@
70
68
 
71
69
  $whereSql = implode( " AND ", $where );
72
70
 
73
- $sql = "select * from $usertable where " . $whereSql;
71
+ $sql = "select * from TABLE1 where " . $whereSql;
74
72
 
75
73
  } else {
76
74
 
77
- $sql = "select * from $usertable";
75
+ $sql = "select * from TABLE1";
78
76
 
79
77
  }
80
78
 
@@ -192,17 +190,21 @@
192
190
 
193
191
  } catch ( PDOException $e ) {
194
192
 
195
- die( "接続エラー: {$e->getMessage()}" );
193
+ die( "接続エラー1: {$e->getMessage()}" );
196
194
 
197
195
  }
198
196
 
199
197
  return $db;
200
198
 
201
-
199
+  //ここまでは接続確認済です。
200
+
201
+
202
202
 
203
203
  //queryのデータ
204
204
 
205
+ try {
206
+
205
- $query = "SELECT * FROM $usertable WHERE search_name LIKE ? AND color LIKE ? AND category LIKE ?";
207
+   $query = "SELECT * FROM TABLE1 WHERE search_name LIKE ? ESCAPE '!' AND color LIKE ? ESCAPE '!' AND category LIKE ? ESCAPE '!' ";
206
208
 
207
209
  $qPart = array_fill( 0, count( $userData ), "(?, ?, ?, ?, ?)" );
208
210
 
@@ -226,13 +228,15 @@
226
228
 
227
229
  foreach ( $userData as $row ) { //bind the values one by one
228
230
 
231
+ $stmt->bindValue( $i++, $row[ '%' . preg_replace( '/(?=[!_%])/', '!', $search_name ) . '%' ], PDO::PARAM_STR );
232
+
229
- $UserDataSet->bindValue( $i++, $row[ '%' . addcslashes( $search_name, '\_%' ) . '%' ], PDO::PARAM_STR );
233
+ $stmt->bindValue( $i++, $row[ '%' . preg_replace( '/(?=[!_%])/', '!', $color ) . '%' ], PDO::PARAM_STR );
230
-
231
- $UserDataSet->bindValue( $i++, $row[ '%' . addcslashes( $color, '\_%' ) . '%' ], PDO::PARAM_STR );
234
+
232
-
233
- $UserDataSet->bindValue( $i++, $row[ '%' . addcslashes( $category, '\_%' ) . '%' ], PDO::PARAM_STR );
235
+ $stmt->bindValue( $i++, $row[ '%' . preg_replace( '/(?=[!_%])/', '!', $category ) . '%' ], PDO::PARAM_STR );
234
-
236
+
235
- }
237
+ }
238
+
239
+ $UserDataSet->execute();
236
240
 
237
241
  //扱いやすい形に変える
238
242
 
@@ -246,7 +250,13 @@
246
250
 
247
251
  return $result;
248
252
 
253
+
254
+
249
- $UserDataSet->execute();
255
+ } catch ( PDOException $e ) {
256
+
257
+ die( "接続エラー2: {$e->getMessage()}" );
258
+
259
+ }
250
260
 
251
261
  }
252
262