PHP & MySQL

07. 글수정

drizzle0925 2021. 3. 26. 23:16
728x90

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
46
47
48
49
<?php
$conn = mysqli_connect(
  'localhost',
  'root',
  '111111',
  'tutorials');
 
$sql = "SELECT * FROM topic";
$result = mysqli_query($conn$sql);
$list = '';
while($row = mysqli_fetch_array($result)) {
  $escaped_title = htmlspecialchars($row['title']);
  $list = $list."<li><a href=\"index.php?id={$row['id']}\">{$escaped_title}</a></li>";
}
 
$article = array(
  'title'=>'Welcome',
  'description'=>'Hello, web'
);
$update_link = '';
if(isset($_GET['id'])) {
  $filtered_id = mysqli_real_escape_string($conn$_GET['id']);
  $sql = "SELECT * FROM topic WHERE id={$filtered_id}";
  $result = mysqli_query($conn$sql);
  $row = mysqli_fetch_array($result);
  $article['title'= htmlspecialchars($row['title']);
  $article['description'= htmlspecialchars($row['description']);
 
  $update_link = '<a href="update.php?id='.$_GET['id'].'">update</a>';
}
 
?>
<!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>
    <?=$update_link?>
    <h2><?=$article['title']?></h2>
    <?=$article['description']?>
  </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
45
46
47
48
49
<?php
$conn = mysqli_connect(
  'localhost',
  'root',
  '111111',
  'tutorials');
 
$sql = "SELECT * FROM topic";
$result = mysqli_query($conn$sql);
$list = '';
while($row = mysqli_fetch_array($result)) {
  $escaped_title = htmlspecialchars($row['title']);
  $list = $list."<li><a href=\"index.php?id={$row['id']}\">{$escaped_title}</a></li>";
}
 
$article = array(
  'title'=>'Welcome',
  'description'=>'Hello, web'
);
$update_link = '';
if(isset($_GET['id'])) {
  $filtered_id = mysqli_real_escape_string($conn$_GET['id']);
  $sql = "SELECT * FROM topic WHERE id={$filtered_id}";
  $result = mysqli_query($conn$sql);
  $row = mysqli_fetch_array($result);
  $article['title'= htmlspecialchars($row['title']);
  $article['description'= htmlspecialchars($row['description']);
 
  $update_link = '<a href="update.php?id='.$_GET['id'].'">update</a>';
}
 
?>
<!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>
    <?=$update_link?>
    <h2><?=$article['title']?></h2>
    <?=$article['description']?>
  </body>
</html>
cs

 

update.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
46
47
48
49
50
51
<?php
$conn = mysqli_connect(
  'localhost',
  'root',
  '111111',
  'tutorials');
 
$sql = "SELECT * FROM topic";
$result = mysqli_query($conn$sql);
$list = '';
while($row = mysqli_fetch_array($result)) {
  $escaped_title = htmlspecialchars($row['title']);
  $list = $list."<li><a href=\"index.php?id={$row['id']}\">{$escaped_title}</a></li>";
}
 
$article = array(
  'title'=>'Welcome',
  'description'=>'Hello, web'
);
$update_link = '';
if(isset($_GET['id'])) {
  $filtered_id = mysqli_real_escape_string($conn$_GET['id']);
  $sql = "SELECT * FROM topic WHERE id={$filtered_id}";
  $result = mysqli_query($conn$sql);
  $row = mysqli_fetch_array($result);
  $article['title'= htmlspecialchars($row['title']);
  $article['description'= htmlspecialchars($row['description']);
 
  $update_link = '<a href="update.php?id='.$_GET['id'].'">update</a>';
}
 
?>
<!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_update.php" method="POST">
      <input type="hidden" name="id" value="<?=$_GET['id']?>">
      <p><input type="text" name="title" placeholder="title" value="<?=$article['title']?>"></p>
      <p><textarea name="description" placeholder="description"><?=$article['description']?></textarea></p>
      <p><input type="submit"></p>
    </form>
  </body>
</html>
cs

 

process_update.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
<?php
$conn = mysqli_connect(
  'localhost',
  'root',
  '111111',
  'tutorials');
 
settype($_POST['id'], 'integer');
$filtered = array(
  'id'=>mysqli_real_escape_string($conn$_POST['id']),
  'title'=>mysqli_real_escape_string($conn$_POST['title']),
  'description'=>mysqli_real_escape_string($conn$_POST['description'])
);
 
$sql = "
  UPDATE topic
    SET
      title = '{$filtered['title']}',
      description = '{$filtered['description']}'
    WHERE
      id = {$filtered['id']}
";
$result = mysqli_query($conn$sql);
if($result === false){
  echo '저장하는 과정에서 문제가 생겼습니다. 관리자에게 문의해주세요';
  error_log(mysqli_error($conn));
else {
  echo '성공했습니다. <a href="index.php">돌아가기</a>';
}
?>
cs
728x90

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

09. PHP MySQL CRUD 애플리케이션 만들기(Procedural)  (0) 2021.10.24
08. 글삭제  (0) 2021.03.27
06. PHP & MySQL 보안  (0) 2021.03.26
05. 글읽기  (0) 2021.03.18
04. PHP와 MySQL의 연동과 SELECT  (0) 2021.03.18