あおいさん専用ページ

<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>データ登録</title>
</head>

<body>
  <?php
  if (isset($_POST['page_title'])) {
    $page_title = $_POST['page_title'];
  }
  if (isset($_POST['article_title'])) {
    $article_title = $_POST['article_title'];
  }
  if (isset($_POST['article_content'])) {
    $article_content = $_POST['article_content'];
  }
  $count = 0;
  try {
    $db = new PDO('mysql:dbname=test;host=localhost;charset=utf8', 'root', 'iluv2playt3m1s');

    // 記事が投稿されたときだけ、insert.phpに表示するURLを増やす
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $count = $db->exec('INSERT INTO test (page_title, article_title, article_content) values ("' . $page_title . '","' . $article_title . '","' . $article_content . '")');
    }

    $sql = "SELECT * FROM test";
    $sth = $db->query($sql);
    $count = $sth->rowCount();
  } catch (PDOException $e) {
    echo 'DB接続エラー' . $e->getMessage();
  }
  ?>
  <h2>生成済みのページ</h2>

  <?php if ($count > 0) : ?>
    <?php foreach (range(1, $count) as $i) : ?>
      <a href="http://localhost/article.php?id=<?php echo $i; ?>">http://localhost/article.php?id=<?php echo $i; ?></a><br>
    <?php endforeach; ?>
  <?php endif; ?>
</body>

</html>
タイトルとURLをコピーしました