MySQL & PHP 실습 (4)

yurison·2022년 12월 30일
0
  • index.php
<?php
$conn = mysqli_connect("localhost", "root", "111111");
mysqli_select_db($conn, "database_name");
$result = mysqli_query($conn, "SELECT * FROM table_name");
?>

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

<head>
  <meta charset="UTF-8" />
  <link rel="stylesheet" href="http://localhost/project_php/style.css" />
  <title>PHP Project</title>
</head>

<body id="target">
  <header>
    <h1>
      <a href="http://localhost/project_php/index.php">JavaScript</a>
    </h1>
  </header>
  <nav>
    <ul>
      <?php
      while ($row = mysqli_fetch_assoc($result)) {
        echo "<li><a href='http://localhost/index.php?id=" . $row['id'] . "'>" . $row['title'] . "</a></li>" . "\n";
      }
      ?>
    </ul>
  </nav>
  <div>
    <input type="button" value="white" onclick="document.getElementById('target').className='white'" />
    <input type="button" value="black" onclick="document.getElementById('target').className='black'" />
    <a href="http://localhost/write.php">Write</a>
  </div>
  <article>
    <?php
    if (empty($_GET['id']) === false) {
      $sql = "SELECT * FROM table_name WHERE id=" . $_GET['id'];
      $result = mysqli_query($conn, $sql);
      $row = mysqli_fetch_assoc($result);
      echo '<h2>' . $row['title'] . '</h2>';
      echo $row['description'];
    }
    ?>
  </article>
</body>

</html>
  • write.php
<?php
$conn = mysqli_connect("localhost", "root", "111111");
mysqli_select_db($conn, "database_name");
$result = mysqli_query($conn, "SELECT * FROM table_name");
?>

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

<head>
  <meta charset="UTF-8" />
  <link rel="stylesheet" href="http://localhost/project_php/style.css" />
  <title>PHP Project</title>
</head>

<body id="target">
  <header>
    <h1>
      <a href="http://localhost/project_php/index.php">JavaScript</a>
    </h1>
  </header>
  <nav>
    <ul>
      <?php
      while ($row = mysqli_fetch_assoc($result)) {
        echo "<li><a href='http://localhost/index.php?id=" . $row['id'] . "'>" . $row['title'] . "</a></li>" . "\n";
      }
      ?>
    </ul>
  </nav>
  <div>
    <input type="button" value="white" onclick="document.getElementById('target').className='white'" />
    <input type="button" value="black" onclick="document.getElementById('target').className='black'" />
    <a href="http://localhost/write.php">Write</a>
  </div>
  <article>
    <form action="process.php" method="POST">
      <p>
        <label for="title">제목 : </label>
        <input type="text" id='title' name="title">
      </p>
      <p>
        <label for="author">작성자 : </label>
        <input type="text" id='author' name="author">
      </p>
      <p>
        <label for="maintext">본문 : </label>
        <textarea name="description" id="maintext" cols="30" rows="10"></textarea>
        <input type="submit">
      </p>
    </form>
  </article>
</body>

</html>
  • process.php
<?php
    $conn = mysqli_connect("localhost", "root", "111111");
    mysqli_select_db($conn, "database_name");
    $sql = "INSERT INTO table_name (title, description, author, created) VALUES ('".$_POST['title']."', '".$_POST['description']."', '".$_POST['author']."', '".now())";
    $result = mysqli_query($conn, $sql);
    header('Location: http://localhost/index.php');
?>

0개의 댓글