質問編集履歴

1

文法の訂正

2021/07/01 16:05

投稿

gentakun2
gentakun2

スコア1

test CHANGED
File without changes
test CHANGED
@@ -213,3 +213,235 @@
213
213
  ```
214
214
 
215
215
  よろしくお願いいたします。
216
+
217
+
218
+
219
+ 追加情報
220
+
221
+ 直した部分
222
+
223
+ queryを使わずにprepareで準備してからexecuteで実行した。
224
+
225
+ $_POSTではなく$_GETを利用するようにした。
226
+
227
+
228
+
229
+ ソースコード
230
+
231
+ ```html
232
+
233
+ <!DOCTYPE html>
234
+
235
+ <html lang="ja">
236
+
237
+ <head>
238
+
239
+ <meta charset="utf-8">
240
+
241
+ <title>PHP TEST</title>
242
+
243
+ </head>
244
+
245
+ <body>
246
+
247
+ <form action="testphp.php" method="get">
248
+
249
+ 検索ワード:<input type="text" name="word" />
250
+
251
+ <input type="submit" />
252
+
253
+ </form>
254
+
255
+
256
+
257
+
258
+
259
+ </body>
260
+
261
+ </html>
262
+
263
+
264
+
265
+ ```
266
+
267
+
268
+
269
+ ```php
270
+
271
+
272
+
273
+ <?php
274
+
275
+
276
+
277
+ define('max_view',10);
278
+
279
+ $word = $_GET["word"];
280
+
281
+
282
+
283
+ $servername = "localhost";
284
+
285
+ $username = "shizutaro";
286
+
287
+ $password = " ";
288
+
289
+ $dbname = "CSexp1DB";
290
+
291
+ $port = "80";
292
+
293
+
294
+
295
+ // Create connection
296
+
297
+ $conn = new mysqli($servername, $username, $password,$dbname,$port);
298
+
299
+
300
+
301
+ // Check connection
302
+
303
+ if ($conn->connect_error) {
304
+
305
+ die("Connection failed: " . $conn->connect_error);
306
+
307
+ }
308
+
309
+
310
+
311
+ $word = htmlspecialchars($word);
312
+
313
+
314
+
315
+ if(is_numeric($word)){
316
+
317
+ $result = $conn->prepare("SELECT * FROM zipShizuoka WHERE zip like '%" .?. "%' ");
318
+
319
+ $result->bind_param('s',$word);
320
+
321
+ $result->execute();
322
+
323
+
324
+
325
+ }else{
326
+
327
+ $result = $conn->prepare("SELECT * FROM zipShizuoka WHERE CONCAT(kana1,kana2,kana3,addr1,addr2,addr3) like '%" .?. "%'");
328
+
329
+ $result->bind_param('s',$word);
330
+
331
+ $result->execute();
332
+
333
+ }
334
+
335
+
336
+
337
+ printf("%d",$result->affected_rows);
338
+
339
+ $total_count = $result->affected_rows;
340
+
341
+ $pages = ceil($total_count / max_view);
342
+
343
+
344
+
345
+ if(!isset($_GET['page_id'])){
346
+
347
+ $now = 1;
348
+
349
+ }else{
350
+
351
+ $now = $_GET['page_id'];
352
+
353
+ }
354
+
355
+
356
+
357
+ if(is_numeric($word)){
358
+
359
+ if($now == 1){
360
+
361
+ $result1 = $conn->prepare("SELECT * FROM zipShizuoka WHERE zip like '%" .?. "%' LIMIT 0,10");
362
+
363
+ $result1->bind_param('s',$word);
364
+
365
+ $result1->execute();
366
+
367
+ }else{
368
+
369
+ $result1 = $conn->prepare("SELECT * FROM zipShizuoka WHERE zip like '%" .?. "%' LIMIT ?,10");
370
+
371
+ $start = ($now-1)*10;
372
+
373
+ $result1->bind_param('si',$word,$start);
374
+
375
+ $result1->execute();
376
+
377
+ }
378
+
379
+ }else{
380
+
381
+ if($now == 1){
382
+
383
+ $result1 = $conn->prepare("SELECT * FROM zipShizuoka WHERE CONCAT(kana1,kana2,kana3,addr1,addr2,addr3) like '%" .?. "%'LIMIT 0,10 ");
384
+
385
+ $result1->bind_param('s',$word);
386
+
387
+ $result1->execute();
388
+
389
+ }else {
390
+
391
+ $result1 = $conn->prepare("SELECT * FROM zipShizuoka WHERE CONCAT(kana1,kana2,kana3,addr1,addr2,addr3) like '%" .?. "%'LIMIT ?,10");
392
+
393
+ $result1->bind_param('si',$word,$start);
394
+
395
+ $result1->execute();
396
+
397
+ }
398
+
399
+ }
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+ echo "<br>";
408
+
409
+ foreach($result1 as $row){
410
+
411
+ echo $row['zip'] . '' .$row['addr1'] . '' .$row['addr2'] . '' .$row['addr3'];
412
+
413
+ echo "<br>";
414
+
415
+ }
416
+
417
+
418
+
419
+ for ( $n = 1; $n <= $pages; $n ++){
420
+
421
+ if ( $n == $now ){
422
+
423
+ echo "<span style='padding: 5px;'>$now</span>";
424
+
425
+ }else{
426
+
427
+ echo "<a href='./testphp.php?page_id=$n&word=",urldecode($_GET["word"]),"' style='padding: 5px;'>$n</a>";
428
+
429
+ }
430
+
431
+ }
432
+
433
+
434
+
435
+ $conn = $conn->close($conn);
436
+
437
+
438
+
439
+ ?>
440
+
441
+ </body>
442
+
443
+ </html>
444
+
445
+
446
+
447
+ ```