質問編集履歴
2
ログイン情報の変更
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -1,168 +1,125 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            整理のため再度投稿します。
         
     | 
| 
       2 
     | 
    
         
            -
            PHPで 
     | 
| 
      
 2 
     | 
    
         
            +
            PHPで投稿機能を実装しています。
         
     | 
| 
       3 
3 
     | 
    
         | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
       4 
5 
     | 
    
         
             
            参考にしているサイトは、以下のサイトです。
         
     | 
| 
       5 
6 
     | 
    
         
             
            https://www.techpit.jp/courses/132/curriculums/135/sections/976/parts/3946
         
     | 
| 
       6 
7 
     | 
    
         | 
| 
       7 
     | 
    
         
            -
            こちらを見て 
     | 
| 
      
 8 
     | 
    
         
            +
            こちらを見て投稿機能を作成しているのですが、不明点として投稿する度にuser_idが0として扱われます。
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
       8 
10 
     | 
    
         
             
            試したこととしては、requireで読み込んでいる部分(/common/auth.phpやfunction.php、database.phpなどをコメントアウトしてみる)をやってみました。この処理がまとまっていないために起きている不具合だと考えましたが、こちら原因わかる方いますか?
         
     | 
| 
       9 
11 
     | 
    
         | 
| 
       10 
12 
     | 
    
         
             
            ログ出力してみると以下のような結果が出ました。
         
     | 
| 
       11 
13 
     | 
    
         | 
| 
       12 
14 
     | 
    
         
             
            ```ここに言語を入力
         
     | 
| 
       13 
     | 
    
         
            -
            [21-Feb-2021  
     | 
| 
       14 
     | 
    
         
            -
            [21-Feb-2021 06:53:31 UTC] PHP Notice:  Undefined index: pass in /Applications/MAMP/htdocs/working_space/users/login.php on line 120
         
     | 
| 
       15 
     | 
    
         
            -
            [21-Feb-2021 06:53:34 UTC] PHP Notice:  session_start(): A session had already been started - ignoring in /Applications/MAMP/htdocs/aaaa/common/function.php on line 18
         
     | 
| 
       16 
     | 
    
         
            -
            [21-Feb-2021 06:53:34 UTC] PHP Notice:  Undefined index: pass in /Applications/MAMP/htdocs/aaaa/users/login.php on line 120
         
     | 
| 
       17 
     | 
    
         
            -
            [21-Feb-2021 06:53:42 UTC] PHP Notice:  session_start(): A session had already been started - ignoring in /Applications/MAMP/htdocs/aaaa/common/function.php on line 18
         
     | 
| 
       18 
     | 
    
         
            -
            [21-Feb-2021 06:53:42 UTC] PHP Notice:  session_start(): A session had already been started - ignoring in /Applications/MAMP/htdocs/aaaa/common/function.php on line 18
         
     | 
| 
       19 
     | 
    
         
            -
            [21-Feb-2021 06:53:42 UTC] PHP Notice:  Undefined index: pass in /Applications/MAMP/htdocs/aaaa/users/login.php on line 120
         
     | 
| 
      
 15 
     | 
    
         
            +
            [21-Feb-2021 08:15:39 UTC] PHP Warning:  PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /Applications/MAMP/htdocs/aaaa/tweets/create.php on line 37
         
     | 
| 
       20 
16 
     | 
    
         
             
            ```
         
     | 
| 
       21 
17 
     | 
    
         | 
| 
       22 
18 
     | 
    
         
             
            ```ここに言語を入力
         
     | 
| 
       23 
     | 
    
         
            -
            // 
     | 
| 
      
 19 
     | 
    
         
            +
            //tweets/create.php
         
     | 
| 
       24 
20 
     | 
    
         | 
| 
       25 
21 
     | 
    
         
             
            <?php
         
     | 
| 
       26 
     | 
    
         
            -
                
         
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
      
 22 
     | 
    
         
            +
            require('../common/auth.php');
         
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
                if(isLogin()) {
         
     | 
| 
       30 
     | 
    
         
            -
                    header('Location: ../tweets/index.php');
         
     | 
| 
       31 
     | 
    
         
            -
                    exit;
         
     | 
| 
       32 
     | 
    
         
            -
                }
         
     | 
| 
       33 
     | 
    
         
            -
            ?>
         
     | 
| 
       34 
     | 
    
         
            -
            <?php
         
     | 
| 
       35 
23 
     | 
    
         
             
            require('../common/function.php');
         
     | 
| 
       36 
24 
     | 
    
         
             
            require('../common/database.php');
         
     | 
| 
       37 
25 
     | 
    
         
             
            require('../common/head_info.php');
         
     | 
| 
      
 26 
     | 
    
         
            +
            if (!isLogin()) {
         
     | 
| 
      
 27 
     | 
    
         
            +
              header('Location: ../login/');
         
     | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
      
 28 
     | 
    
         
            +
              exit;
         
     | 
| 
      
 29 
     | 
    
         
            +
            }
         
     | 
| 
       39 
30 
     | 
    
         | 
| 
      
 31 
     | 
    
         
            +
            $user_id = getLoginUserId();
         
     | 
| 
      
 32 
     | 
    
         
            +
            $database_handler = getDatabaseConnection();
         
     | 
| 
       40 
33 
     | 
    
         | 
| 
       41 
     | 
    
         
            -
            <?php 
         
     | 
| 
       42 
     | 
    
         
            -
            // 
     | 
| 
      
 34 
     | 
    
         
            +
              //post送信されていた場合
         
     | 
| 
       43 
35 
     | 
    
         
             
            if(!empty($_POST)) {
         
     | 
| 
      
 36 
     | 
    
         
            +
              //バリデーションチェック
         
     | 
| 
      
 37 
     | 
    
         
            +
              $title = (isset($_POST['title'])) ? $_POST['title'] : '';
         
     | 
| 
      
 38 
     | 
    
         
            +
              $content = (isset($_POST['content'])) ? $_POST['content'] : '';
         
     | 
| 
      
 39 
     | 
    
         
            +
              // //最大文字数チェック
         
     | 
| 
      
 40 
     | 
    
         
            +
              // validMaxLen($comment, 'comment');
         
     | 
| 
       44 
     | 
    
         
            -
               
     | 
| 
      
 41 
     | 
    
         
            +
              // //未入力チェック
         
     | 
| 
       45 
     | 
    
         
            -
               
     | 
| 
      
 42 
     | 
    
         
            +
              // validNotEntered($comment, 'comment');
         
     | 
| 
       46 
43 
     | 
    
         | 
| 
       47 
     | 
    
         
            -
              //未入力チェック
         
     | 
| 
       48 
     | 
    
         
            -
              validateNot($email,'email');
         
     | 
| 
       49 
     | 
    
         
            -
              validateNot($pass,'pass');
         
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
44 
     | 
    
         
             
              if(empty($err_msg)) {
         
     | 
| 
      
 45 
     | 
    
         
            +
                  //例外処理
         
     | 
| 
      
 46 
     | 
    
         
            +
                  try {
         
     | 
| 
      
 47 
     | 
    
         
            +
                      $user_id = getLoginUserId();
         
     | 
| 
      
 48 
     | 
    
         
            +
                      print_r('debug',$user_id);
         
     | 
| 
      
 49 
     | 
    
         
            +
                      //DB接続 
         
     | 
| 
      
 50 
     | 
    
         
            +
                      $database_handler = getDatabaseConnection();
         
     | 
| 
      
 51 
     | 
    
         
            +
                      // プリペアドステートメントで SQLをあらかじめ用意しておく
         
     | 
| 
      
 52 
     | 
    
         
            +
                      $statement = $database_handler->prepare('INSERT INTO tweets (user_id, title, content) VALUES (:user_id, :title, :content)');
         
     | 
| 
      
 53 
     | 
    
         
            +
                      //指定された変数名にパラメータをバインド(紐付け)
         
     | 
| 
      
 54 
     | 
    
         
            +
                      $statement->bindParam(':title', $title);
         
     | 
| 
      
 55 
     | 
    
         
            +
                      $statement->bindParam(':user_id', $user_id);
         
     | 
| 
      
 56 
     | 
    
         
            +
                      $statement->execute();
         
     | 
| 
       52 
57 
     | 
    
         | 
| 
      
 58 
     | 
    
         
            +
                      $_SESSION['select_tweet'] = [
         
     | 
| 
      
 59 
     | 
    
         
            +
                        'id' => $database_handler->lastInsertId(),
         
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
      
 60 
     | 
    
         
            +
                        'title' => $title,
         
     | 
| 
      
 61 
     | 
    
         
            +
                        'content' => $content,
         
     | 
| 
      
 62 
     | 
    
         
            +
                    ];
         
     | 
| 
       54 
63 
     | 
    
         | 
| 
       55 
     | 
    
         
            -
                //メールアドレスの形式チェック
         
     | 
| 
       56 
64 
     | 
    
         | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
      
 65 
     | 
    
         
            +
                      //クエリ成功の場合
         
     | 
| 
      
 66 
     | 
    
         
            +
                      if($statement) {
         
     | 
| 
       58 
     | 
    
         
            -
             
     | 
| 
      
 67 
     | 
    
         
            +
                          $_POST = array(); //postをクリア
         
     | 
| 
       59 
     | 
    
         
            -
                //パスワードの最小文字数チェック
         
     | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
      
 68 
     | 
    
         
            +
                          header('Location:../tweets/community01.php'); //自分自身に遷移する
         
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
      
 69 
     | 
    
         
            +
                          exit();
         
     | 
| 
      
 70 
     | 
    
         
            +
                      }
         
     | 
| 
       62 
71 
     | 
    
         | 
| 
       63 
     | 
    
         
            -
             
     | 
| 
      
 72 
     | 
    
         
            +
                  } catch(Exception $e) {
         
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
                  $database_handler = getDatabaseConnection();
         
     | 
| 
       66 
     | 
    
         
            -
                  // プリペアドステートメントで SQLをあらかじめ用意しておく
         
     | 
| 
       67 
     | 
    
         
            -
                  if ($statement = $database_handler->prepare('SELECT id, name, password FROM users WHERE email = :email')) {
         
     | 
| 
       68 
     | 
    
         
            -
                     $statement->bindParam(':email', $email);
         
     | 
| 
       69 
     | 
    
         
            -
                     $statement->execute();
         
     | 
| 
       70 
     | 
    
         
            -
                     $user = $statement->fetch();
         
     | 
| 
       71 
     | 
    
         
            -
                     
         
     | 
| 
       72 
     | 
    
         
            -
                    if (!$user) {
         
     | 
| 
       73 
     | 
    
         
            -
                        $_SESSION['errors'] = [
         
     | 
| 
       74 
     | 
    
         
            -
                            'メールアドレスまたはパスワードが間違っています。'
         
     | 
| 
       75 
     | 
    
         
            -
                        ];
         
     | 
| 
       76 
     | 
    
         
            -
                        header('Location: ../users/login.php');
         
     | 
| 
       77 
     | 
    
         
            -
                        exit;
         
     | 
| 
       78 
     | 
    
         
            -
                    }
         
     | 
| 
       79 
     | 
    
         
            -
             
     | 
| 
       80 
     | 
    
         
            -
                    $name = $user['name'];
         
     | 
| 
       81 
     | 
    
         
            -
                    $id = $user['id'];
         
     | 
| 
       82 
     | 
    
         
            -
             
     | 
| 
       83 
     | 
    
         
            -
                    if (password_verify($pass, $user['pass'])) {
         
     | 
| 
       84 
     | 
    
         
            -
                        // ユーザー情報保持
         
     | 
| 
       85 
     | 
    
         
            -
                        $_SESSION['user'] = [
         
     | 
| 
       86 
     | 
    
         
            -
                            'name' => $name,
         
     | 
| 
       87 
     | 
    
         
            -
                            'id' => $id
         
     | 
| 
       88 
     | 
    
         
            -
                        ];
         
     | 
| 
       89 
     | 
    
         
            -
             
     | 
| 
       90 
     | 
    
         
            -
                        // 更新日が最新のメモ情報保持
         
     | 
| 
       91 
     | 
    
         
            -
                        if ($statement = $database_handler->prepare("SELECT id, title, content FROM memos WHERE user_id = :user_id ORDER BY updated_at DESC LIMIT 1")) {
         
     | 
| 
       92 
     | 
    
         
            -
                            $statement->bindParam(":user_id", $id);
         
     | 
| 
       93 
     | 
    
         
            -
                            $statement->execute();
         
     | 
| 
       94 
     | 
    
         
            -
                            $result = $statement->fetch(PDO::FETCH_ASSOC);
         
     | 
| 
       95 
     | 
    
         
            -
             
     | 
| 
       96 
     | 
    
         
            -
                            if ($result) {
         
     | 
| 
       97 
     | 
    
         
            -
                                $_SESSION['select_tweets'] = [
         
     | 
| 
       98 
     | 
    
         
            -
                                    'id' => $result['id'],
         
     | 
| 
       99 
     | 
    
         
            -
                                    'title' => $result['title'],
         
     | 
| 
       100 
     | 
    
         
            -
                                    'content' => $result['content']
         
     | 
| 
       101 
     | 
    
         
            -
                                ];
         
     | 
| 
       102 
     | 
    
         
            -
                            }
         
     | 
| 
       103 
     | 
    
         
            -
                        }
         
     | 
| 
       104 
     | 
    
         
            -
             
     | 
| 
       105 
     | 
    
         
            -
             
     | 
| 
      
 73 
     | 
    
         
            +
                      error_log('エラー発生:'. $e->getMessage());
         
     | 
| 
       106 
     | 
    
         
            -
                        exit;
         
     | 
| 
       107 
     | 
    
         
            -
                    } else {
         
     | 
| 
       108 
     | 
    
         
            -
                        $_SESSION['errors'] = [
         
     | 
| 
       109 
     | 
    
         
            -
                            'メールアドレスまたはパスワードが間違っています。'
         
     | 
| 
       110 
     | 
    
         
            -
                        ];
         
     | 
| 
       111 
     | 
    
         
            -
                        header('Location: ../users/login.php');
         
     | 
| 
       112 
     | 
    
         
            -
                        exit;
         
     | 
| 
       113 
     | 
    
         
            -
                    }
         
     | 
| 
       114 
74 
     | 
    
         
             
                  }
         
     | 
| 
       115 
     | 
    
         
            -
                }
         
     | 
| 
       116 
75 
     | 
    
         
             
              }
         
     | 
| 
       117 
76 
     | 
    
         
             
            }
         
     | 
| 
       118 
     | 
    
         
            -
            ?> 
     | 
| 
      
 77 
     | 
    
         
            +
            ?>
         
     | 
| 
       119 
78 
     | 
    
         | 
| 
       120 
79 
     | 
    
         
             
            <?php
         
     | 
| 
       121 
80 
     | 
    
         
             
            require('../common/header.php');
         
     | 
| 
       122 
81 
     | 
    
         
             
            ?>
         
     | 
| 
       123 
82 
     | 
    
         | 
| 
      
 83 
     | 
    
         
            +
            <?php
         
     | 
| 
      
 84 
     | 
    
         
            +
                if(!empty($_SESSION['user']['id'])) {
         
     | 
| 
      
 85 
     | 
    
         
            +
              ?>
         
     | 
| 
       124 
     | 
    
         
            -
            <div class= 
     | 
| 
      
 86 
     | 
    
         
            +
            <div class="main-top">
         
     | 
| 
       125 
     | 
    
         
            -
              <div class='form-login'>
         
     | 
| 
       126 
     | 
    
         
            -
                <div class="form-login-list">
         
     | 
| 
       127 
     | 
    
         
            -
                  <h2><i class="fas fa-sign-in-alt"></i>ログイン</h2>
         
     | 
| 
       128 
     | 
    
         
            -
                  <section class='guestuser'>
         
     | 
| 
       129 
     | 
    
         
            -
                    ゲストユーザー用
         
     | 
| 
       130 
     | 
    
         
            -
                    <p><i class="far fa-envelope"></i>メールアドレス:guest@mail.com</p>
         
     | 
| 
       131 
     | 
    
         
            -
                    <p><i class="fas fa-unlock-alt"></i>パスワード:guestuser</p>
         
     | 
| 
       132 
     | 
    
         
            -
                  </section>
         
     | 
| 
       133 
     | 
    
         
            -
             
     | 
| 
       134 
     | 
    
         
            -
             
     | 
| 
      
 87 
     | 
    
         
            +
                <form action="" method='post' class='form review-form'>
         
     | 
| 
      
 88 
     | 
    
         
            +
                  <div class='button-containers'>
         
     | 
| 
      
 89 
     | 
    
         
            +
                    <div class="tweet-header">zzz</div>
         
     | 
| 
      
 90 
     | 
    
         
            +
                    <div class="tweet-body">
         
     | 
| 
      
 91 
     | 
    
         
            +
                      <div class="tweet-tops">
         
     | 
| 
       135 
     | 
    
         
            -
             
     | 
| 
      
 92 
     | 
    
         
            +
                        <label>
         
     | 
| 
       136 
     | 
    
         
            -
             
     | 
| 
      
 93 
     | 
    
         
            +
                          <input type="text" name='title' placeholder="タイトル">
         
     | 
| 
       137 
     | 
    
         
            -
             
     | 
| 
      
 94 
     | 
    
         
            +
                          <div class="error_mes">
         
     | 
| 
       138 
     | 
    
         
            -
             
     | 
| 
      
 95 
     | 
    
         
            +
                          <?php 
         
     | 
| 
       139 
     | 
    
         
            -
             
     | 
| 
      
 96 
     | 
    
         
            +
                          if(!empty($err_msg['pass'])) echo $err_msg['pass'];
         
     | 
| 
       140 
     | 
    
         
            -
             
     | 
| 
      
 97 
     | 
    
         
            +
                          ?>
         
     | 
| 
      
 98 
     | 
    
         
            +
                          </div>
         
     | 
| 
      
 99 
     | 
    
         
            +
                        </label>
         
     | 
| 
      
 100 
     | 
    
         
            +
                        <label>
         
     | 
| 
      
 101 
     | 
    
         
            +
                          <textarea name="content"  cols="82" rows="10" class='review-textarea'></textarea>
         
     | 
| 
      
 102 
     | 
    
         
            +
                          <div class="error_mes">
         
     | 
| 
      
 103 
     | 
    
         
            +
                          <?php 
         
     | 
| 
      
 104 
     | 
    
         
            +
                          if(!empty($err_msg['pass'])) echo $err_msg['pass'];
         
     | 
| 
      
 105 
     | 
    
         
            +
                          ?>
         
     | 
| 
      
 106 
     | 
    
         
            +
                          </div>
         
     | 
| 
      
 107 
     | 
    
         
            +
                        </label>
         
     | 
| 
      
 108 
     | 
    
         
            +
                        <div class='button-container'>
         
     | 
| 
      
 109 
     | 
    
         
            +
                          <input type="submit" value='投稿する'>
         
     | 
| 
       141 
110 
     | 
    
         
             
                        </div>
         
     | 
| 
       142 
     | 
    
         
            -
                      </label>
         
     | 
| 
       143 
     | 
    
         
            -
                      <label>
         
     | 
| 
       144 
     | 
    
         
            -
                        <input type="password" name='pass' placeholder="パスワード"  value="<?php print(htmlspecialchars($_POST['pass'],ENT_QUOTES)); ?>"></br>
         
     | 
| 
       145 
     | 
    
         
            -
                        <div class="error_mes">
         
     | 
| 
       146 
     | 
    
         
            -
                        <?php 
         
     | 
| 
       147 
     | 
    
         
            -
                        if(!empty($err_msg['pass'])) echo $err_msg['pass'];
         
     | 
| 
       148 
     | 
    
         
            -
                        ?>
         
     | 
| 
       149 
     | 
    
         
            -
             
     | 
| 
      
 111 
     | 
    
         
            +
                      </div>
         
     | 
| 
       150 
     | 
    
         
            -
                        <span class='form-rule'>※英数字8文字以上</span>
         
     | 
| 
       151 
     | 
    
         
            -
                      </label>
         
     | 
| 
       152 
     | 
    
         
            -
                      <label>
         
     | 
| 
       153 
     | 
    
         
            -
                        <input type="checkbox" name='pass_save'>次回ログインを省略する
         
     | 
| 
       154 
     | 
    
         
            -
                      </label>
         
     | 
| 
       155 
     | 
    
         
            -
                    <div class='button-container'>
         
     | 
| 
       156 
     | 
    
         
            -
                      <input type="submit" value='ログインする'>
         
     | 
| 
       157 
112 
     | 
    
         
             
                    </div>
         
     | 
| 
      
 113 
     | 
    
         
            +
                  </div>
         
     | 
| 
       158 
     | 
    
         
            -
             
     | 
| 
      
 114 
     | 
    
         
            +
                </form>
         
     | 
| 
       159 
     | 
    
         
            -
             
     | 
| 
      
 115 
     | 
    
         
            +
            </div>
         
     | 
| 
       160 
     | 
    
         
            -
             
     | 
| 
      
 116 
     | 
    
         
            +
             
     | 
| 
       161 
     | 
    
         
            -
            </div> 
         
     | 
| 
       162 
     | 
    
         
            -
                
         
     | 
| 
       163 
117 
     | 
    
         
             
            <?php 
         
     | 
| 
       164 
118 
     | 
    
         
             
             require('../common/footer.php');
         
     | 
| 
       165 
     | 
    
         
            -
            ?> 
     | 
| 
      
 119 
     | 
    
         
            +
            ?>  
         
     | 
| 
      
 120 
     | 
    
         
            +
            <?php
         
     | 
| 
      
 121 
     | 
    
         
            +
                }
         
     | 
| 
      
 122 
     | 
    
         
            +
              ?> 
         
     | 
| 
       166 
123 
     | 
    
         
             
            ```
         
     | 
| 
       167 
124 
     | 
    
         | 
| 
       168 
125 
     | 
    
         
             
            ```ここに言語を入力
         
     | 
1
DB2の削除
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         
            File without changes
         
     |