PHPの学習において、掲示板を作成していたのですが、データベースに格納しています値を、表示する際に、少しうまくいかず解決できない部分がある為、お力をお貸しいただけましたら幸いです。
以下は、データベースの値を表示する部分である、「bbs.php」になります。
<?php require_once 'config.php'; require_once 'db_connect.php'; $page = 0; $num = 10; if(isset($_GET['page']) && $_GET['page'] > 0){ $page = intval($_GET['page']) -1; } $db = db_connection(DSN,DB_USERNAME,DB_PASSWORD); $stmt = $db->prepare(" SELECT name,title,comment,date FROM post_2 ORDER BY date DESC LIMIT :page, :num "); $page = $page * $num; $stmt->bindValue(':num',$num,PDO::PARAM_INT); $stmt->bindValue(':page',$page,PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetch(); var_dump($result); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>掲示板</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="main.css"> <script src="main.js"></script> </head> <body> <h1>掲示板</h1> <form action="write.php" method="post"> <p>名前:<input type="text" name="name"></p> <p>タイトル:<input type="text" name="title"></p> <textarea name="comment" cols="50" rows="5"></textarea> <p>削除パスワード(数字4桁):<input type="text" name="password"></p> <input type="submit"value="書き込む"> </form> <hr> <?php while($row = $stmt->fetch()): $title = $row['title'] ? $row['title'] : '(無題)'; ?> <p>名前:<?= $row['name'] ?></p> <p>タイトル:<?= $title ?></p> <p>本文:<?= $row['comment'] ?></p> <p>日付:<?= $row['date'] ?></p> <?php endwhile; ?> </body> </html>
以下は設定ファイルである、「config.php」です。
php
1<?php 2error_reporting(E_ALL); 3ini_set("display_errors",1); 4define('DSN','mysql:host=localhost;dbname=online_bbs;charset=utf8'); 5define('DB_USERNAME', 'root'); 6define('DB_PASSWORD', '12345');
以下はPDOインスタンスを生成する処理などをまとめた、「db_connect.php」です
php
1<?php 2function db_connection($dsn,$user,$password){ 3 $db = new PDO($dsn,$user,$password); 4 $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 5 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); 6 return $db; 7}
以下はデータベースにデーターを格納する処理を記載した、「write.php」です。
php
1<?php 2 3require_once 'db_connect.php'; 4require_once 'config.php'; 5 6$err = []; 7 8if(!empty($_POST)){ 9 10 $name = filter_input(INPUT_POST,'name',FILTER_SANITIZE_SPECIAL_CHARS); 11 $comment = filter_input(INPUT_POST,'comment',FILTER_SANITIZE_SPECIAL_CHARS); 12 $password = filter_input(INPUT_POST,'password',FILTER_SANITIZE_SPECIAL_CHARS); 13 $title = filter_input(INPUT_POST,'title',FILTER_SANITIZE_SPECIAL_CHARS); 14 15 if(!$name){ 16 $err['name'] = '名前が入力されていません'; 17 } 18 19 if(!$comment){ 20 21 $err['comment'] = '本文が入力されていません'; 22 23 } 24 25 if(!$password){ 26 27 $err['password'] = 'パスワードが入力されていません'; 28 29 }elseif(!preg_match("/^[0-9]{4}$/",$password)) 30 { 31 $err['password'] = 'パスワードは数字4文字で入力してください'; 32 } 33 34 if(!$title){ 35 36 if(strlen($title) > 255) 37 { 38 $err['comment'] = '本文は255文字以内で入力して下さい'; 39 } 40 41 } 42 43 if(empty($err)){ 44 45 try{ 46 47 $db = db_connection(DSN,DB_USERNAME,DB_PASSWORD); 48 $stmt = $db->prepare(" 49 INSERT INTO post_2 (name,title,comment,date,password) 50 VALUES(:name,:title,:comment,now(),:password) 51 "); 52 53 $stmt->bindValue(':name',$name,PDO::PARAM_STR); 54 $stmt->bindValue(':title',$title,PDO::PARAM_STR); 55 $stmt->bindValue(':comment',$comment,PDO::PARAM_STR); 56 $stmt->bindValue(':password',$password,PDO::PARAM_STR); 57 $stmt->execute(); 58 header('Location:bbs.php'); 59 exit(); 60 }catch(PDOException $e){ 61 62 die('エラー:' . $e->getMessage()); 63 64 } 65 66 } 67 68} 69 70?> 71<!DOCTYPE html> 72<html> 73<head> 74 <meta charset="utf-8"> 75 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 76 <title>Page Title</title> 77 <meta name="viewport" content="width=device-width, initial-scale=1"> 78 <link rel="stylesheet" type="text/css" media="screen" href="main.css"> 79 <script src="main.js"></script> 80</head> 81<body> 82 <?php if(count($err) >= 1): ?> 83 <h3>エラー!</h3> 84 85 <?php foreach($err as $value): ?> 86 87 <p> 88 <?=$value?> 89 </p> 90 91 <?php endforeach; ?> 92 93 <?php endif;?> 94</body> 95</html> 96
データベースのテーブル内に格納されている値は、以下の2つです。
引っかかってしまっている部分なのですが、「bbs.php」のwhile文の中で、テーブルに格納されている2つのデータを表示したいのですが、画面には1つ目のデータしか表示されません。さらに、プリペアードステートメントを設定する際に、「ORDER BY date」としているにも関わらず、日付が古い、「id」が1のデーターが表示されてしまっている状態です。
上記2点につきまして、ご助言頂けましたら幸いです。よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/05 02:40
2019/04/05 02:44