#問題点
他のページでは、同じ$.postのコードでも実行されるのに、ある特定のページの$.postだけ上手く処理されません。
原因を特定しているところ、$.postの送る先に問題がありそうです。
どうしても原因がわからないので、よろしくお願いします。
#ソースコード
php
1<?php 2 3ini_set('display_errors', 1); 4 5define('MAX_FILE_SIZE', 1 * 1024 * 1024); //1MB 6define('THUMBNAIL_WIDTH', 400); 7define('IMAGES_DIR', __DIR__ . '/images'); 8define('THUMBNAIL_DIR', __DIR__ . '/thumbs'); 9 10if (!function_exists('imagecreatetruecolor')) { 11 echo "GD not installed"; 12 exit; 13} 14 15 16require 'ImageUploader.php'; 17$uploader = new \MyApp\ImageUploader(); 18 19require '_setting.php'; 20$setting = new \MyApp\Setting(); 21 22 23list($success, $error) = $uploader->getResults(); 24 25$images = $uploader->getImages(); 26 27 28require_once(__DIR__ . '/Login/config/config.php'); 29 30//ユーザーデータ取得 31$app = new \MyApp\Controller\Index(); 32$app->run(); 33 34$id = $app->me()->id; 35 36//いいね済み処理 37require_once(__DIR__ . '/Liked.php'); 38$liked = new \MyApp\Liked(); 39 40 41if($_SERVER['REQUEST_METHOD'] === 'POST') { 42 if (isset($_POST['introduction'])) { 43 $setting->postIntro($id); 44 } else { 45 $uploader->upload($id); 46 } 47} 48?> 49 50<!DOCTYPE html> 51<html lang="ja"> 52 <head> 53 <meta charset="utf-8"> 54 <title>My Page</title> 55 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous"> 56 <link rel="stylesheet" href="mypage_styles.css"> 57 </head> 58 59 <body> 60 61 <header class="bg"> 62 <div class="container"> 63 <h1><a href="logined_index.php" style="text-decoration: none;">***</a></h1> 64 <a href="logined_index.php"><i class="fas fa-home"> TOP</i></a> 65 <div class="logined"> 66 <a href="/Login/public_html/index.php"><i class="fas fa-sign-out-alt" id="<?= $id ?>"> Log Out</i></a> 67 <p class="line">|</p> 68 <a href="setting.php"><i class="fas fa-cog"> 設定</i></a> 69 </div> 70 </div> 71 </header> 72 73 <h1>My Page</h1> 74 75 <section class="profile"> 76 <div class="myIcon"> 77 78 </div> 79 <p class="user_name"><?= h($app->me()->name); ?></p> 80 <p class="introduction"><?php 81 $sql = "SELECT introduction from users where id = :id;"; 82 $stmt = $pdo->prepare($sql); 83 $stmt->bindValue(':id', $id, \PDO::PARAM_INT); 84 $stmt->execute(); 85 $result = $stmt->fetch(\PDO::FETCH_ASSOC); 86 87 echo $result['introduction']; 88 ?></p> 89 90 </section> 91 92 93 <!-- アップロード --> 94 <div class="btn"> 95 Upload! 96 <form action="" method="post" enctype="multipart/form-data" id="my_form"> 97 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo h(MAX_FILE_SIZE); ?>"> 98 <input type="file" name="image" id="my_file"> 99 </form> 100 </div> 101 102 <?php if (isset($success)) : ?> 103 <div class="msg success"><?php echo h($success); ?></div> 104 <?php endif; ?> 105 106 <?php if (isset($error)) : ?> 107 <div class="msg error"><?php echo h($error); ?></div> 108 <?php endif; ?> 109 110 111 <section class="bottom"> 112 <p class="like">Like</p> 113 <p class="posted">My Posted</p> 114 <div class="list"> 115 <ul class="img_row"> 116 <?php 117 $sql = "SELECT * FROM posts where user_id = $id order by id desc;"; 118 $stmt = $pdo->prepare($sql); 119 $stmt -> execute(); 120 while ($row = $stmt -> fetch(\PDO::FETCH_ASSOC)): 121 ?> 122 <li id="img_<?= $row[id] ?>"> 123 <?php 124 echo ("<a href='/images/$row[file_name]'>"); 125 echo ("<img src='/thumbs/$row[file_name]' class='img'></a>"); 126 $liked->exists($row['id'], $id); 127 echo ("<div class='likes_count'> $row[likes_count] </div>"); 128 echo ("<i class='far fa-trash-alt'></i>"); 129 ?> 130 </li> 131 <?php endwhile; ?> 132 </ul> 133 </div> 134 </section> 135 136 <footer class="bg"> 137 <p>© </p> 138 </footer> 139 140 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 141 <script> 142 $(function() { 143 'use strict'; 144 145 146 $('#my_file').on('change', function() { 147 $('#my_form').submit(); 148 }); 149 150 $( '.icon' ).on( 'click', function() { 151 var $_t = $( this ); 152 var $_u = $('.user'); 153 $_t.css( 'color', 'red' ); 154 $.post( 'Countup.php', { 155 post_id: $_t.parent().find( 'div' ).attr( 'id' ), 156 user_id: $_u.parent().find('span').attr('id') 157 }, function( data ) { 158 $_t.parent().find( '.likes_count' ).text( data ); 159 } ); 160 } ); 161 162 $( '.fa-trash-alt' ).on( 'click', function() { 163 var $_t = $( this ); 164 var $_u = $('.fa-sign-out-alt'); 165 if(confirm('削除しますがよろしいですか?')) { 166 $.post( 'Delete_post.php', { 167 post_id: $_t.parent().find( 'div' ).attr( 'id' ), 168 user_id: $_u.attr('id') 169 }, function() { 170 $('#img_' + post_id).fadeOut(800); 171 }); 172 } 173 }); 174 175 }); 176 177 </script> 178 </body> 179</html> 180
php
1<?php 2 3require_once(__DIR__ . '/Login/config/config.php'); 4 5if ($_SERVER['REQUEST_METHOD'] === 'POST') { 6 try { 7 8 if (!isset($_POST['post_id'])) { 9 throw new \Exception('post_id not set!'); 10 } 11 12 if (!isset($_POST['user_id'])) { 13 throw new \Exception('user_id not set!'); 14 } 15 16 $post_id = $_POST['post_id']; 17 $user_id = $_POST['user_id']; 18 19 // var_dump($post_id); 20 // exit; 21 22 //Validate (post_idとuser_idがuniqueなペアかどうか) 23 $sql = "SELECT * from posts where id = :post_id and user_id = :user_id;"; 24 $stmt = $this->pdo->prepare($sql); 25 $stmt->bindValue(":id", $post_id, \PDO::PARAM_INT); 26 $stmt->bindValue(":user_id", $user_id, \PDO::PARAM_INT); 27 $stmt->execute(); 28 $result = $stmt->fetch(\PDO::FETCH_ASSOC); 29 30 31 if(empty($result)) { 32 throw new \Exception('No post!'); 33 } else { 34 35 $sql = "DELETE from posts where id = :post_id and user_id = :user_id;"; 36 $stmt = $this->pdo->prepare($sql); 37 $stmt->bindValue(":post_id", $post_id, \PDO::PARAM_INT); 38 $stmt->bindValue(":user_id", $user_id, \PDO::PARAM_INT); 39 $stmt->execute(); 40 41 $sql = "DELETE from likes where id = :post_id;"; 42 $stmt = $this->pdo->prepare($sql); 43 $stmt->bindValue(":post_id", $post_id, \PDO::PARAM_INT); 44 $stmt->execute(); 45 46 47 } 48 } catch (Exception $e) { 49 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); 50 echo $e->getMessage(); 51 exit; 52 } 53} 54 55 ?> 56
回答1件
あなたの回答
tips
プレビュー