๐์ฃผ์ ๋ฉ์๋
GET POST
-url ํ๋ผ๋ฏธํฐ๋ฅผ ํตํด์(GET๋ฐฉ์) ์ฌ์ดํธ์ ์ ์ํ๋๊ฑด
๋ถ๋งํฌ ํ ๋ ์ ํฉํ๋ค.
-url์ ํตํ์ง ์๊ณ ์๋ฐํ๊ฒ ๋ฐ์ดํฐ ์ ์ก(POST)
file_put_contents('data/'.$_POST['title'], $_POST['description']);
ํ์ผ ์ ์ฅ ๋ฉ์๋ (ํ์ผ๊ฒฝ๋ก.ํ์ผ๋ช
, ํ์ผ๋ด์ฉ)
๋ฆฌ๋ค์ด๋ ์
์ฌ์ฉ์๋ฅผ ํน์ ํ์ด์ง๋ก ๋ณด๋ด๋ ๊ฒ
header('Location: index.php?id='.$_POST['title']);
ํ์ผ๋ช
์์ /๋ณ๊ฒฝ
rename('data/'.$_POST['old_title'], 'data/'.$_POST['title']);
ํ์ผ ์ญ์
unlink('data/'.$_POST['id']);
index.php
ํ๋ฉด ํ๋จ CRUD๊ธฐ๋ฅ ๋งํฌ ๋ฐ ๋ฒํผ ์์ฑ
<?php
function print_title() {
if(isset($_GET['id'])) {
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description() {
if(isset($_GET['id'])) {
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list() {
$list = scandir('./data');
$i = 0;
while($i < count($list)) {
if($list[$i] != ".") {
if($list[$i] != "..") {
echo "<li><a href='index.php?id=$list[$i]'>$list[$i]</a></li>\n";
}
}
$i++;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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=<?=$_GET['id']?>">update</a>
<form action="delete_process.php" method="POST">
<input type="hidden" name="id" value="<?=$_GET['id']?>">
<input type="submit" value="delete">
</form>
<?php
}
?>
<h2>
<?php
print_title();
?>
</h2>
<?php
print_description();
?>
</body>
</html>
if ์กฐ๊ฑด๋ฌธ ์์ aํ๊ทธ์ formํ๊ทธ๋ฅผ ์์น์์ผ
id๊ฐ์ด ์์ ๊ฒฝ์ฐ์๋ง ์์ , ์ญ์ ๊ฐ ๊ฐ๋ฅํ๋๋ก ๋ง๋ฌ

create.php
form ํ๊ทธ๋ง ๋ด๋ ๋จ
<?php
function print_title() {
if(isset($_GET['id'])) {
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description() {
if(isset($_GET['id'])) {
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list() {
$list = scandir('./data');
$i = 0;
while($i < count($list)) {
if($list[$i] != ".") {
if($list[$i] != "..") {
echo "<li><a href='index.php?id=$list[$i]'>$list[$i]</a></li>\n";
}
}
$i++;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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>
<form action="create_process.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>
create_process.php๋ก post๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ์ ์ก
create_process.php
<?php
// ํ์ผ ์์ฑ
file_put_contents('data/'.$_POST['title'], $_POST['description']);
// ๋ฆฌ๋ค์ด๋ ์
header('Location: /index.php?id='.$_POST['title']);
?>
update.php
form ํ๊ทธ๋ง ๋ด๋ ๋จ
<?php
function print_title() {
if(isset($_GET['id'])) {
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description() {
if(isset($_GET['id'])) {
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list() {
$list = scandir('./data');
$i = 0;
while($i < count($list)) {
if($list[$i] != ".") {
if($list[$i] != "..") {
echo "<li><a href='index.php?id=$list[$i]'>$list[$i]</a></li>\n";
}
}
$i++;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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=<?=$_GET['id']?>">update</a>
<?php
}
?>
<h2>
<?php
print_title();
?>
</h2>
<?php
print_description();
?>
<form action="update_process.php" method="POST">
<input type="hidden" name="old_title" value="<?=$_GET['id']?>">
<p>
<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>
update_process.php๋ก post๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ์ ์ก
์์ ์ title์ "old_title"์ด๋ผ๋ ๋ณ์์ ๋ด์์ inputํ๊ทธ์ hiddenํ์ ์ผ๋ก value๊ฐ(id) ์ ์ก
update_process.php
<?php
// ํ์ผ๋ช
์์ /๋ณ๊ฒฝ
rename('data/'.$_POST['old_title'], 'data/'.$_POST['title']);
// ์์ ๋ ํ์ผ์ descriptionํ์ผ์ ๋ณ๊ฒฝ
file_put_contents('data/'.$_POST['title'], $_POST['description']);
// ๋ฆฌ๋ค์ด๋ ์
header('Location: /index.php?id='.$_POST['title']);
?>
delete_process.php
<?php
unlink('data/'.$_POST['id']);
header('Location: /index.php');
?>
ํ์ผ ์ญ์ ํ index.php๋ก ๋ฆฌ๋ค์ด๋ ์
'PHP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| PHP ๋ณด์ ํ์ผ ๊ฒฝ๋ก ๋ณดํธ (0) | 2023.01.24 |
|---|---|
| PHP ํ์ผ๋ก ๋ชจ๋ํ (0) | 2023.01.24 |
| PHP ๊ธฐ์ด4 (0) | 2023.01.20 |
| PHP ๊ธฐ์ด3 (0) | 2023.01.20 |
| PHP ๊ธฐ์ด2 (0) | 2023.01.19 |
๋๊ธ