PHP & MySQL

05. 글읽기

drizzle0925 2021. 3. 18. 20:55
728x90

index.php

데이터가 추가 혹은 삭제됨에 따라 ol태그의 리스트가 동적으로 변하도록 수정하자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
$conn = mysqli_connect(
  'localhost',
  'root',
  '111111',
  'tutorials');
 
$sql = "SELECT * FROM topic";
$result = mysqli_query($conn$sql);
$list = '';
while($row = mysqli_fetch_array($result)) {
  $list = $list."<li><a href=\"index.php?id={$row['id']}\">{$row['title']}</a></li>";
}
?>
 
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>WEB</title>
  </head>
  <body>
    <h1>WEB</h1>
    <ol>
        <?=$list?>
    </ol>
    <a href="create.php">create</a>
    <h2>Welcome</h2>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit
  </body>
</html>
cs

index.php

화면에 출력할 데이터를 데이터베이스에서 가져오기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
$conn = mysqli_connect(
  'localhost',
  'root',
  '111111',
  'tutorials');
 
$sql = "SELECT * FROM topic";
$result = mysqli_query($conn$sql);
$list = '';
while($row = mysqli_fetch_array($result)) {
  $list = $list."<li><a href=\"index.php?id={$row['id']}\">{$row['title']}</a></li>";
}
 
$article = array(
    'title'=>'Welcome',
    'description'=>'Hello, web'
  );
  if(isset($_GET['id'])) {
    $sql = "SELECT * FROM topic WHERE id={$_GET['id']}";
    $result = mysqli_query($conn$sql);
    $row = mysqli_fetch_array($result);
    $article['title'= $row['title'];
    $article['description'= $row['description'];
  }
 
?>
 
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>WEB</title>
  </head>
  <body>
    <h1><a href="index.php">WEB</a></h1>
    <ol>
        <?=$list?>
    </ol>
    <a href="create.php">create</a>
    <h2><?=$article['title']?></h2>
    <?=$article['description']?>
  </body>
</html>
cs

 

create.php

선택한 데이터를 데이터베이스에서 가져오기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
$conn = mysqli_connect(
  'localhost',
  'root',
  '111111',
  'tutorials');
 
$sql = "SELECT * FROM topic";
$result = mysqli_query($conn$sql);
$list = '';
while($row = mysqli_fetch_array($result)) {
  $list = $list."<li><a href=\"index.php?id={$row['id']}\">{$row['title']}</a></li>";
}
 
$article = array(
  'title'=>'Welcome',
  'description'=>'Hello, web'
);
if(isset($_GET['id'])) {
  $sql = "SELECT * FROM topic WHERE id={$_GET['id']}";
  $result = mysqli_query($conn$sql);
  $row = mysqli_fetch_array($result);
  $article['title'= $row['title'];
  $article['description'= $row['description'];
}
 
?>
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>WEB</title>
  </head>
  <body>
    <h1><a href="index.php">WEB</a></h1>
    <ol>
        <?=$list?>
    </ol>
    <form action="process_create.php" method="POST">
      <p><input type="text" name="title" placeholder="title"></p>
      <p><textarea name="description" placeholder="description"></textarea></p>
      <p><input type="submit"></p>
    </form>
  </body>
</html>
cs
728x90

'PHP & MySQL' 카테고리의 다른 글

07. 글수정  (0) 2021.03.26
06. PHP & MySQL 보안  (0) 2021.03.26
04. PHP와 MySQL의 연동과 SELECT  (0) 2021.03.18
03. 글생성  (0) 2021.03.18
02. PHP와 MySQL의 연동  (0) 2021.03.17