๊ด€๋ฆฌ ๋ฉ”๋‰ด

C-log

๐Ÿ˜์›น์‚ฌ์ดํŠธ์˜ ๋น„ํ•˜์ธ๋“œ PHP : Edit ๋ณธ๋ฌธ

DB/๐Ÿ˜PHP

๐Ÿ˜์›น์‚ฌ์ดํŠธ์˜ ๋น„ํ•˜์ธ๋“œ PHP : Edit

4:Bee 2023. 9. 8. 17:51
728x90

์ด๋ฒˆ์—๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ƒ์„ฑํ•œ ํŒŒ์ผ๋“ค๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ํŽธ์ง‘ํ•˜๊ณ  ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ๋“ค์„ ๊ตฌํ˜„ํ•ด๋ณผ ๊ฒƒ์ด๋‹ค. ์šฐ์„  ์šฐ๋ฆฌ๊ฐ€ update.php์™€ update_process.phpํŒŒ์ผ ๋‘๊ฐœ๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค. ๊ธฐ์กด์˜ index.php์—์„œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

<?php if (isset($_GET['id'])) { ?>
    <a href="update.php?id=<?php echo $_GET['id']; ?>">update</a>
  <?php } ?>

๋จผ์ € update.php ํŒŒ์ผ์„ ์„คํŽด๋ณด์ž 

update.php

<?php
function print_title()
{...}

function print_description()
{...}

function print_list()
{...}

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

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>
    <?php
    print_title();
    ?>
  </title>
</head>

<body>
  <h1><a href="index.php">WEB</a></h1>
  <ol>
    <?php
    print_list();
    ?>
  </ol>
  <a href="create.php">create</a>
  <?php if (isset($_GET['id'])) { ?>
    <a href="update.php?id=<?php echo $_GET['id']; ?>">update</a>
  <?php } ?>

  <form action="update_process.php" method="post">
    <input type="hidden" name="old_title" value="<?= $_GET['id'] ?>">
    <p>
      <!-- ์—ฌ๊ธฐ์„œ ์ˆ˜์ •ํ•˜์ง€๋งŒ ํ•ด๋‹น php์˜ ๊ฐ’์„ value๋กœ ์ง์ ‘ ์ž‘์„ฑ -->
      <input type="text" name="title" placeholder="Title" value="<?php print_title(); ?>">
    </p>
    <p>
      <textarea name="description" placeholder="Description"><?php print_description(); ?></textarea>
    </p>
    <p>
      <input type="submit">
    </p>
  </form>
</body>

</html>

์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” aํƒœ๊ทธ์™€ formํƒœ๊ทธ๋ฅผ ์ฃผ์˜ ๊นŠ๊ฒŒ ๋ณผํ•„์š”๊ฐ€ ์žˆ๋‹ค. ์ด์ „์—๋„ ์‚ฌ์šฉํ–ˆ๋˜ ์ฝ”๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ ‡๊ฒŒ ํฌ๊ฒŒ ์–ด๋ ต์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๋‹ค. ๋‹ค๋งŒ inputํƒœ๊ทธ์—์„œ hidden ํƒ€์ž…์ธ ๋ถ€๋ถ„์„ ์ž˜ ์ดํ•ดํ•ด์•ผํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.


input type="hidden"...

 
<input type="hidden" name="old_title" value="<?= $_GET['id'] ?>">
 

inputํƒœ๊ทธ์˜ value๋ฅผ ํ˜„ idํƒœ๊ทธ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด inputํƒœ๊ทธ์˜ ํ™œ์šฉ์€ update_process.php์˜ renameํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. ์•„๋ž˜ update_process.php ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž

update_process.php

<?php
rename('data/' . $_POST['old_title'], 'data/' . $_POST['title']);
file_put_contents('data/' . $_POST['title'], $_POST['description']);
header('Location: index.php?id=' . $_POST['title']);

์šฐ๋ฆฌ๊ฐ€ file_put_contentsํ•จ์ˆ˜๋‚˜ headerํ•จ์ˆ˜๋Š” ์ด์ „ ๊ฐ•์˜ ์‹œ๊ฐ„์—์„œ ๊ฐ€๋ณ๊ฒŒ ๋‹ค๋ฃจ์—ˆ์—ˆ๋‹ค. renameํ•จ์ˆ˜ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ํŒŒ์ผ๋ช…์„ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋ณ€๊ฒฝ๋œ ๋‚ด์šฉ์„ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋ถ€๋ถ„์€ ์•„๋‹Œ ๊ฒƒ์ด๋‹ค. ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ถ€๋ถ„์€ file_put_contentsํ•จ์ˆ˜์—์„œ ์ด๋ฃจ์–ด์ง€๋Š”๋ฐ ์‚ฌ์‹ค์ƒ ๋ณ€๊ฒฝ์ด ์•„๋‹ˆ๋ผ ๋‚ด์šฉ์„ ๋ฎ์–ด ์“ฐ๋Š” ๊ฒƒ์ด๋‹ค.

728x90
Comments