フォームから投稿した投稿が表示されません
そもそも保存できていないと思いますが、何が原因なのか全くわかりません
よろしくお願いします。
※OSはM1macで、phpmyadminを使ってローカルに建てたapacheのwebサーバーで動かそうとしています。ブラウザはchromeでエディタはVScodeを使っています。
SQLはmySQLです。
全てバージョンは現時点で最新のものにアップデートしています。
PHP
1<?php 2 3date_default_timezone_set("Asia/Tokyo"); 4 5$comment_array = array(); 6$pdo = null; 7$stmt = null; 8$error_messages = array(); 9 10//DB接続 11try { 12 $pdo = new PDO('mysql:host=localhost;dbname=bbs-yt', "root", "root"); 13} catch (PDOException $e) { 14 echo $e->getMessage(); 15} 16 17//フォームを打ち込んだ時 18 if(!empty($_POST["submitButton"])){ 19 20 //名前のチェック 21 if(empty($post["username"])){ 22 echo "名前を入力してください"; 23 $error_messages["username"] = "名前を入力してください"; 24 } 25 26 //コメントのチェック 27 if(empty($post["comment"])){ 28 echo "コメントを入力してください"; 29 $error_messages["comment"] = "コメントを入力してください"; 30 } 31 32 33 34 35 if(empty($error_messages)){ 36 $postDate = date("Y-m-d H:i:s"); 37 try { 38 $stmt = $pdo->prepare("INSERT INTO 'bbs-table' ('username', 'comment', 'postDate') VALUES (:username, :comment, :postDate)"); 39 $stmt->bindParam(':username', $_POST["username"], PDO::PARAM_STR); 40 $stmt->bindParam(':comment', $_POST["comment"], PDO::PARAM_STR); 41 $stmt->bindParam(':postDate', $postDate, PDO::PARAM_STR); 42 $stmt->execute(); 43 }catch (PDOException $e) { 44 echo $e->getMessage(); 45} 46 } 47} 48 49 50 51//DBからコメントデータを取得する 52$sql = "SELECT * FROM `bbs-table`;"; 53$comment_array = $pdo->query($sql); 54 55 56//DBの接続を閉じる 57$pdo = null; 58 59 60 61?> 62 63<!DOCTYPE html> 64<html lang="ja"> 65<head> 66 <meta charset="UTF-8"> 67 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 68 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 69 <title>PHP掲示板</title> 70 <link rel="stylesheet" href="style.css"> 71</head> 72<body> 73 <h1 class="title">PHPで掲示板アプリ</h1> 74 <hr> 75 <div class="boardWrapper"> 76 <section> 77 <?php foreach($comment_array as $comment): ?> 78 <article> 79 <div class="wrapper"> 80 <div class="nameArea"> 81 <span>名前:</span> 82 <p class="username"><?php echo $comment["username"]; ?></p> 83 <time>:<?php echo $comment["postDate"]; ?></time> 84 </div> 85 <p class="comment"><?php echo $comment["comment"]; ?></p> 86 </div> 87 </article> 88 <?php endforeach; ?> 89 </section> 90 <form class="formWrapper" method="POST"> 91 <div> 92 <input type="submit" value="書き込む" name="submitButton"> 93 <label for="">名前:</label> 94 <input type="text" name="username"> 95 </div> 96 <div> 97 <textarea class="commentTextArea" name="comment"></textarea> 98 </div> 99 </form> 100 </div> 101</body> 102</html>
CSS
1body { 2 background-color: #c6daf1; 3} 4 5.title { 6 text-align: center; 7 font-size: 34px; 8} 9 10.boardWrapper { 11 /* background-color: #b3eeb3; */ 12 background-color: #f3eded; 13 padding: 20px; 14 max-width: 1000px; 15 margin: 0 auto; 16 border: 1px solid black; 17 margin-bottom: 20px; 18} 19 20section { 21 padding: 0px 10px; 22} 23 24section .username { 25 color: green !important; 26 font-weight: 700; 27} 28 29section .nameArea { 30 display: flex; 31 align-items: center; 32 margin-bottom: -14px; 33} 34 35section .comment { 36 margin-top: 0; 37 margin-left: 20px; 38} 39 40.success_message { 41 color: green; 42 padding-left: 10px; 43} 44 45.error_message { 46 color: red; 47 padding-left: 10px; 48} 49 50.formWrapper { 51 padding: 16px; 52 margin-left: 20px; 53} 54 55.commentTextArea { 56 margin-top: 10px; 57 width: 500px; 58 height: 90px; 59}

回答1件
あなたの回答
tips
プレビュー