前提・実現したいこと
PHPとmySQLで掲示板のようなものを作成途中です。投稿ページ(post.php)から投稿された内容をDBへ格納し、home.phpで投稿された内容をそれぞれ繰り返し表示したいです。 DBにはきちんと格納されています。
発生している問題・エラーメッセージ
下記コードだと最新の投稿内容だけが繰り返し表示されてしまいます。どのように書けばすべての投稿内容を表示することができるでしょうか。なにぶんPHP初学者でして、ご助言頂けると助かります。
該当のソースコード
(home.php): <?php session_start(); include_once('dbconnect.php');//DB接続 //usersテーブルとpostsテーブルをリレーションさせる $query = "SELECT * from posts LEFT JOIN users ON posts.post_id = users.user_id"; $result = $mysqli->query($query); //ポスト情報の取り出し $posteds = $result;//ここらへんがわからないです while ($row = $result->fetch_assoc()) { $username = $row['username']; $photo = $row['photo']; $post_name = $row['post_name']; $post_author = $row['post_author']; $post_introduction = $row['post_introduction']; $post_image = $row['post_image']; } ?> <!doctype html> <html lang="ja"> <head> ~~~省略(Bootstrap使ってます)~~~ </head> <body> <div class="text-center text-white"> <a href="post.php" class="btn btn-lg btn-success"><span><i class="fas fa-book-open"></i></span> 投稿ページへ</a> </div> <div class="post-wrapper pt-5"> <div class="container"> <div class="row"> <!-- 繰り返し表示したい箇所 --> <?php foreach ($posteds as $posted): ?> <div class="col-md-4 text-center mb-3"> <img src="<?php echo $post_image; ?>" width="200px" height="300px"> </div> <div class="col-md-8 mb-3"> <h3><?php echo $post_name; ?></h3> <p><?php echo $post_author; ?></p> <p><?php echo $post_introduction; ?></p> </div> <div class="col-md-4 text-center mb-3"> <img src="<?php echo $photo; ?>" width="120px" height="120px" class="rounded-circle"> <p>紹介者 : <?php echo $username; ?>さん</p> </div> <div class="col-12"> <hr style="background-color:#EB6964" class="mb-5" > </div> <?php endforeach; ?> <!-- ここまで繰り返し --> </div> </div> </div> </body> </html>
(post.php): <?php session_start(); include_once('dbconnect.php'); //DB接続 //投稿ボタンを押したときの処理 if (isset($_POST['posting'])) { $post_name = $mysqli->real_escape_string($_POST['post_name']); $post_author = $mysqli->real_escape_string($_POST['post_author']); $post_introduction = $mysqli->real_escape_string($_POST['post_introduction']); $post_image = "bookimage/".$_FILES['post_image']['name']; //uses_idをpost_idに代入 $query2 = "SELECT user_id FROM users WHERE user_id=".$_SESSION['user'].""; $result = $mysqli->query($query2); while ($row = $result->fetch_assoc()) { $post_id = $row['user_id']; } //POSTされた情報をDBへ格納する $query = "INSERT INTO posts SET post_name=?, post_author=?, post_introduction=?, post_image=?, post_id=?"; $stmt = $mysqli->prepare($query); $stmt->bind_param("ssssi", $post_name, $post_author, $post_introduction, $post_image, $post_id); $stmt->execute(); move_uploaded_file($_FILES['post_image']['tmp_name'], $post_image); } ?> <!doctype html> <html lang="ja"> <head> ~~~省略(Bootstrap使ってます)~~~ </head> <body> <div class="post-wrapper pt-5"> <div class="container"> <div class="row"> <div class="col-md-6 offset-md-3 text-center"> <h2>投稿画面</h2> <form method="post" enctype="multipart/form-data"> <label class="col-form-label">タイトル <span class="bg-danger text-white p-1" >必須</span></label> <input type="text" name="post_name" class="form-control" placeholder="例) ちはやふる" required/> <label class="col-form-label">作者</label> <input type="text" name="post_author" class="form-control" placeholder="例) 末次 由紀"> <label class="col-form-label">画像 <span class="bg-danger text-white p-1" >必須</span></label> <input type="file" name="post_image" class="form-control" required/> <label for="exampleTextarea">紹介文</label> <textarea name="post_introduction" class="form-control" placeholder="例) 最高に面白いです!" rows="8" cols="80"></textarea> <div class="text-center"> <input type="submit" name="posting" class="btn btn-lg btn-success mt-4" value="投稿する"> <a href="home.php">戻る</a> </div> </form> </div> </div> </div> </div> </body> </html>
補足情報
DBにはusersテーブルとpostsテーブルを作成しています。
・usersテーブル
user_id(AI), username, email, password, photo
・postsテーブル
id(AI), post_name, post_author, post_introduction, post_image, post_id
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。