Help us understand the problem. What is going on with this article?

PHPでシンプルな掲示板を製作

More than 1 year has passed since last update.

PHPで単純な掲示板を製作。idで降順(最新が上位にくる)になるように、またリロード時の二重投稿への対策はしています。

データベースlocaldbのuserテーブルのデータはid(A_I), name, commentのみです。

index.php
<!DOCTYPE HTML>
<html lang="ja">

<head>
</head>

<body>
    <form method="post" action="">
        <input type="text" name="name" value="">
        <input type="text" name="comment" value="">
        <input type="submit" name="regist" value="登録">
    </form>
</body>
<?php
?>
</html>
php
<?php
    $host = 'localhost:8889';
    $dbname = 'localdb';
    $dbuser = 'root';
    $dbpassword = 'root';
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8","$dbuser","$dbpassword");
    #データベースへの登録
    if( isset($_POST['regist']) ){
        $name = $_POST['name'];
        $comment = $_POST['comment'];
        $sql = "INSERT INTO user (name, comment) VALUES ('$name', '$comment')";
        $res = $pdo->query($sql);
    }
    #テーブル内容の表示
    $sql = "SELECT * FROM user ORDER BY id DESC";
    $stmt = $pdo->query($sql);
    foreach ($stmt as $row) {
        echo $row['name'].' '.$row['comment'];
        echo '<br>';
    }
    #リロード時の二重投稿を防止
    if($_SERVER['REQUEST_METHOD'] === 'POST') {
    header('Location:http://localhost:8888/index.php', true, 303);
    }
    #終了
    $stmt = null;
    $pdo = null;
?>

php部分を改訂してみました。 vol.1

php
<?php
    $host = 'localhost:8889';
    $dbname = 'localdb';
    $dbuser = 'root';
    $dbpassword = 'root';
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8","$dbuser","$dbpassword");
    #データベースへの登録
    if( isset($_POST['regist']) ){
        $name = $_POST['name'];
        $comment = $_POST['comment'];
        $stmt = $pdo -> prepare("INSERT INTO user (name, comment) VALUES (:name, :comment)");
        $stmt->bindParam(':name', $name, PDO::PARAM_STR);
        $stmt->bindParam(':comment', $comment, PDO::PARAM_STR);
        $stmt->execute();
        $pdo->query($stmt);
        header("Location: " . $_SERVER['PHP_SELF']);
    }
    #テーブル内容の表示
    $sql = "SELECT * FROM user ORDER BY id DESC";
    $stmt = $pdo->query($sql);
    foreach ($stmt as $row) {
        echo htmlspecialchars($row['name'], ENT_QUOTES|ENT_HTML5).' '.htmlspecialchars($row['comment'], ENT_QUOTES|ENT_HTML5);
        echo '<br>';
    }
?>
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away