質問編集履歴

1

ソースコード追加

2018/12/02 05:39

投稿

mango
mango

スコア33

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  他のページでは、同じ$.postのコードでも実行されるのに、ある特定のページの$.postだけ上手く処理されません。
4
4
 
5
- 原因を特定しているところ、$.postで値をCountup.phpやDelete_post.phpに送るに問題がありそうです。
5
+ 原因を特定しているところ、$.post送るに問題がありそうです。
6
6
 
7
7
  どうしても原因がわからないので、よろしくお願いします。
8
8
 
@@ -10,6 +10,10 @@
10
10
 
11
11
 
12
12
 
13
+
14
+
15
+ #ソースコード
16
+
13
17
  ```php
14
18
 
15
19
  <?php
@@ -373,3 +377,121 @@
373
377
 
374
378
 
375
379
  ```
380
+
381
+
382
+
383
+ ```php
384
+
385
+ <?php
386
+
387
+
388
+
389
+ require_once(__DIR__ . '/Login/config/config.php');
390
+
391
+
392
+
393
+ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
394
+
395
+ try {
396
+
397
+
398
+
399
+ if (!isset($_POST['post_id'])) {
400
+
401
+ throw new \Exception('post_id not set!');
402
+
403
+ }
404
+
405
+
406
+
407
+ if (!isset($_POST['user_id'])) {
408
+
409
+ throw new \Exception('user_id not set!');
410
+
411
+ }
412
+
413
+
414
+
415
+ $post_id = $_POST['post_id'];
416
+
417
+ $user_id = $_POST['user_id'];
418
+
419
+
420
+
421
+ // var_dump($post_id);
422
+
423
+ // exit;
424
+
425
+
426
+
427
+ //Validate (post_idとuser_idがuniqueなペアかどうか)
428
+
429
+ $sql = "SELECT * from posts where id = :post_id and user_id = :user_id;";
430
+
431
+ $stmt = $this->pdo->prepare($sql);
432
+
433
+ $stmt->bindValue(":id", $post_id, \PDO::PARAM_INT);
434
+
435
+ $stmt->bindValue(":user_id", $user_id, \PDO::PARAM_INT);
436
+
437
+ $stmt->execute();
438
+
439
+ $result = $stmt->fetch(\PDO::FETCH_ASSOC);
440
+
441
+
442
+
443
+
444
+
445
+ if(empty($result)) {
446
+
447
+ throw new \Exception('No post!');
448
+
449
+ } else {
450
+
451
+
452
+
453
+ $sql = "DELETE from posts where id = :post_id and user_id = :user_id;";
454
+
455
+ $stmt = $this->pdo->prepare($sql);
456
+
457
+ $stmt->bindValue(":post_id", $post_id, \PDO::PARAM_INT);
458
+
459
+ $stmt->bindValue(":user_id", $user_id, \PDO::PARAM_INT);
460
+
461
+ $stmt->execute();
462
+
463
+
464
+
465
+ $sql = "DELETE from likes where id = :post_id;";
466
+
467
+ $stmt = $this->pdo->prepare($sql);
468
+
469
+ $stmt->bindValue(":post_id", $post_id, \PDO::PARAM_INT);
470
+
471
+ $stmt->execute();
472
+
473
+
474
+
475
+
476
+
477
+ }
478
+
479
+ } catch (Exception $e) {
480
+
481
+ header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
482
+
483
+ echo $e->getMessage();
484
+
485
+ exit;
486
+
487
+ }
488
+
489
+ }
490
+
491
+
492
+
493
+ ?>
494
+
495
+
496
+
497
+ ```