์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- eport
- Import
- ์ฐธ๊ณ ๋ธ๋ก๊ทธ
- json
- database
- https://m.blog.naver.com/tt2t2am1118/221010125300
- execCommand
- sql
- ajax
- Project
- js
- mysql
- async
- https://youtube.com/playlist?list=PLuHgQVnccGMA5836CvWfieEQy0T0ov6Jh&si=FTaYv8m21EhO-A2K
- ํผํ
- await
- object
- addEventListener
- setTimeout()
- prj
- promise
- slow and steady
- ๋น๋๊ธฐ
- ๋๊ธฐ
- ๊ฒ์
- callback
- JS #ํ๋ก์ ํธ
- webpack
- db
- Porject
- Today
- Total
C-log
๐ฌMySQL&๐PHP : ๋๊ณ ๋์ ์ฝ๋ผ๋ฆฌ์ ์๋ก์ด ์์ ๊ณผ ์ญ์ ๋ณธ๋ฌธ
๐ฌMySQL&๐PHP : ๋๊ณ ๋์ ์ฝ๋ผ๋ฆฌ์ ์๋ก์ด ์์ ๊ณผ ์ญ์
4:Bee 2023. 10. 16. 12:05์ด์ ๋ง์ง๋ง ํํธ์ด๋ค. topic๊ณผ author๋ฅผ ์ฐ๊ฒฐํ๊ณ author์ ๋ฆฌ์คํธ๋ฅผ ์๋ก ์์ฑํ ์ ์์ผ๋ ์์ ๊ณผ ์ญ์ ๋ฅผ ํด๋ณผ ๊ฒ์ด๋ค. ์ด์ ์ topic ๋ถ๋ถ์์ ์์ ๊ณผ ์ญ์ ์ ๋ณ๋ฐ ๋ค๋ฅธ ๊ฒ์ด ์๋ค. ์๋ author.php์ฝ๋๋ฅผ ์ดํด๋ณด์.
<?php
mysqli_report(MYSQLI_REPORT_OFF);
$conn = mysqli_connect(
'localhost',
'root',
'*****',
'phpmysql'
);
?>
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WEB</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<p><a href="index.php">topic</a></p>
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>profile</td>
<td></td>
<?php
$sql = "SELECT * FROM author";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
$filtered = array(
'id' => htmlspecialchars($row['id']),
'name' => htmlspecialchars($row['name']),
'profile' => htmlspecialchars($row['profile']),
)
?>
<tr>
<td><?= $filtered['id'] ?></td>
<td><?= $filtered['name'] ?></td>
<td><?= $filtered['profile'] ?></td>
<td><a href="author.php?id=<?= $filtered['id'] ?>">update</a></td>
<td>
<form action="process_delete_author.php" method="POST" onsubmit="if(!confirm('sure?')){return false;}">
<input type="hidden" name="id" value="<?= $filtered['id'] ?>">
<!-- {<?= $filtered['id'] ?>} ์ฌ๊ธฐ์ ๊ดํธ๋ฅผ ๋ฃ๊ณ ์๋๊ณ ์ ๋ฐ๋ผ์ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ ์ ์๊ณ ์๊ณ ๊ฐ ๊ฒฐ์ ๋๋ค. -->
<input type="submit" value="delete">
</form>
</td>
</tr>
<?php
}
?>
</tr>
</table>
<?php
$escaped = array(
'name' => '',
'profile' => ''
);
$label_submit = 'Create author'; //ํด๋น id๊ฐ์ด ์์ ๊ฒฝ์ฐ์๋ ํด๋น ๊ตฌ๋ฌธ์ผ๋ก
$form_author = 'process_create_author.php'; //ํด๋น id๊ฐ์ด ์์ ๊ฒฝ์ฐ์๋ ํด๋น ๊ตฌ๋ฌธ์ผ๋ก
$form_id = '';
if (isset($_GET['id'])) {
$filtered_id = mysqli_real_escape_string($conn, $_GET['id']);
settype($filtered_id, 'integer');
$sql = "SELECT * FROM author WHERE id = {$filtered_id}";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$escaped['name'] = htmlspecialchars($row['name']);
$escaped['profile'] = htmlspecialchars($row['profile']);
$label_submit = 'Update author';
$form_author = 'process_update_author.php';
$form_id = '<input type="hidden" name="id" value="' . $_GET['id'] . '">';
}
?>
<form action="<?= $form_author ?>" method='POST'>
<!-- ๋์ ์ผ๋ก ๊ฐ๋ค์ด ๋ณํ ์ ์๊ฒ ์ค์ -->
<?= $form_id ?>
<p><input type="text" name="name" placeholder="name" value="<?= $escaped['name'] ?>"></p>
<p><textarea name="profile" placeholder="profile"><?= $escaped['profile'] ?></textarea></p>
<p><input type="submit" value="<?= $label_submit ?>"></p>
</form>
</body>
</html>
์ฌ๊ธฐ์ ์ฐ๋ฆฌ๊ฐ ์ ์ ๊น๊ฒ ๋ด์ผํ ๋ถ๋ถ์ $label_submit๋ณ์์ $form_author๋ณ์์ด๋ค. ์ด ๋ณ์๋ค์ด if๋ฌธ ์กฐ๊ฑด์ ํฉ๋นํ์ง ์์ ๋๋ create์ญํ ๋ก์จ ์กด์ฌํ์ง๋ง if๋ฌธ ์กฐ๊ฑด์ ๋ถํฉํ ์ํ๊ฐ ๋๋ฉด update์ ์ญํ ๋ก ์ ํ์ด ๋๋ค. ๊ฐ ๋ณ์๋ค์ด ์ด๋์ ์ฐ๊ฒฐ๋์ด ์๊ณ ๊ทธ๊ฒ๋ค์ด ์ด๋ค ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋์ง ํ์ธํด ๋ด์ผํ๋ค.
delete์ ์ญ์ ๋ฅผ ๋ด๋นํ๋ tdํ๊ทธ์ form์ ๋ณด๋ฉด hidden ์์ฑ์ ๊ฐ์ง inputํ๊ทธ๊ฐ value ๊ฐ์ผ๋ก <?=$filtered['id'?> ๋ณ์๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ์ฌ๊ธฐ์ ๋ด๊ฐ ํ๋ ์ค์๋ ํด๋น value ์์ฑ์ {}์ด ํ๊ธฐ๋ฅผ ํจ๊ป ์ ์ด ๋ด๋ฉด์ ์ค๋ฅ๋ฅผ ๋ฐ์์์ผฐ์๋ค. ์ด๋ฐ ์ค์๋ก ๋ฐ์ดํฐ๊ฐ ์ ์ก๋๋๋ฐ ์์ด์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์๋ค๋ ๊ฒ์ ์์ง๋ง์. ์ด์ process_update_author.php์ ์ฝ๋๋ฅผ ์ดํด๋ณด์.
<?php
mysqli_report(MYSQLI_REPORT_OFF);
$conn = mysqli_connect(
'localhost',
'root',
'******',
'phpmysql'
);
settype($_POST['id'], 'integer');
$filtered = array(
'id' => mysqli_real_escape_string($conn, $_POST['id']),
'name' => mysqli_real_escape_string($conn, $_POST['name']),
'profile' => mysqli_real_escape_string($conn, $_POST['profile'])
);
$sql = "
UPDATE author
SET
name = '{$filtered['name']}',
profile = '{$filtered['profile']}'
WHERE
id = {$filtered['id']}
";
$result = mysqli_query($conn, $sql);
if ($result === false) {
echo '์ ์ฅํ๋ ๊ณผ์ ์์ ๋ฌธ์ ๊ฐ ์๊ฒผ์ต๋๋ค. ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํด์ฃผ์ธ์.';
error_log(mysqli_error($conn));
} else {
header('Location: author.php?id=' . $filtered['id']);
}
process_update_author.php์ ์ฝ๋๋ ์ฐ๋ฆฌ๊ฐ ์ด์ ์ ๋ดค๋ process_update.php์ ๋งค์ฐ ์ ์ฌํ๋ค ๋ค๋ง $sql๋ณ์์์ ์ฐ๋ฆฌ๊ฐ ์ ํํ TABLE์ด author๊ฐ ๋ง๋์ง ์ํ์ธ ํด์ผํ๋ค. ์ด์ ์ ๋ฐฐ์ ๋ ์ฝ๋์ ํฌ๊ฒ ๋ค๋ฅธ๊ฒ ์์ด์ process_delete_author.php์ฝ๋๋ก ๋์ด๊ฐ์. ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
<?php
mysqli_report(MYSQLI_REPORT_OFF);
$conn = mysqli_connect(
'localhost',
'root',
'******',
'phpmysql'
);
//settype์ ์ฌ์ฉํ๋ ์ด์ ๋ $_POST์ id๊ฐ์ ์ ์๋ก ๊ณ ์ ํ๊ธฐ ์ํ ํจ์์ด๋ค.
settype($_POST['id'], 'integer');
// ์ฌ๊ธฐ array์ ์ญํ ์์ id ๋ฐฐ์ด์ ์ถ๊ฐํด์ค๋ค. ์ฌ๊ธฐ์ filtered์ ์ญํ ์?
$filtered = array(
'id' => mysqli_real_escape_string($conn, $_POST['id']),
);
$sql = "
DELETE FROM author WHERE id = {$filtered['id']}
";
// die($sql);
$result = mysqli_query($conn, $sql);
if ($result === false) {
echo '์ ์ฅํ๋ ๊ณผ์ ์์ ๋ฌธ์ ๊ฐ ์๊ฒผ์ต๋๋ค. ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํด์ฃผ์ธ์.';
error_log(mysqli_error($conn));
} else {
header('Location: author.php');
}
$sql๋ฌธ์ ๋ณด๋ฉด DELETE ๋ช ๋ น๋ฌธ์ ์ฌ์ฉํ์๋ค. ์ฌ๊ธฐ์ ์ฐ๋ฆฌ๊ฐ delete๋ฅผ ํ ๋ ์ฐ๋ฆฌ๊ฐ POST๋ฅผ ํตํด์ ๋ฐ์จ ๋ฐ์ดํฐ ๊ฐ์ด ์ ์ ๋ฌ ๋์๋์ง ํ์ธํ๊ธฐ ์ํด์ die($spl)๋ฌธ์ ํ์ธํ๋ฉด ๋๋ค.
์ด๋ ๊ฒ ๋ชจ๋ ๊ณผ์ ์ ๋ง์ณค๋ค. ์ด ์์ ์ ํตํด์ ์ฐ๋ฆฌ๋ URL๊ฐ์ ๊ฐ์ ธ์ค๊ณ ๋ณด๋ด๋ ๊ฒ์ ์ค์ฌ์ผ๋ก ํ์ด์ง์ ๋ณํ๋ฅผ ์ค๋ค๋ ๊ฒ์ ์ ์ ์์๋ค. sql๋ช ๋ น๋ฌธ์ php์ฝ๋๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์กฐ์ํ ์ ์์๊ณ ์ฝ๋๋ฅผ ํตํด์ ์ง์ ์ ์ผ๋ก MySQL server์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์์๋ค. ํ๋ฐ์๋ ์์ topic TABLE์ ์์ฉํ๊ฑฐ๋ ์ฌ์ฌ์ฉํ๋ ๋ถ๋ถ๋ค์ด ๋ง์์ ๊ธ์ด์ด์ ์ค๋ช ๋ค์ด ๋ง์ด ๋ฏธํกํ๊ณ ๋ถ์กฑํ๋ค. ๊ทธ๋ฌ๋ ๋ ์๋ค์ ์์ ๋ฐฐ์ด ๋ด์ฉ์ ๊ผผ๊ผผํ๊ฒ ์ดํด ๋ณด์๊ธฐ์ ์ด๋ ค์์ด ์์ผ๋ฆฌ๋ผ ๋ฏฟ์ด ์์ฌ์น ์๋ค. ๊ธ์ด์ด๋ ์ด๋ฅผ ํตํด์ ๋ค์ํ ํ๋ก์ ํธ๋ฅผ ์๊ฒ ๋ง๋ค๊ณ ์ฐ์ตํด ๋ณผ ๊ฒ์ด๋ค. ์ด ๊ณผ์ ๋ค ๋ํ ํฌ์คํ ์ ํ ๊ฒ์ด๋ค. ๋์ค์ ๊ด์ฌ์ด ์๋ค๋ฉด ๊ตฌ๊ฒฝ์ค๋ผ ๋ค๋ค ์ฆ๊ฑฐ์ด ์ฝ๋ฉ, ๋ก๊ทธ ๋จ๊ธฐ๊ธธ ๋ฐ๋๋ค.
'Server > ๐ฌMySQL&๐PHP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฌMySQL&๐PHP : ๋๊ณ ๋์ ์ฝ๋ผ๋ฆฌ์ ์๋ก์ด ๊ตฌ์ฑ๊ณผ ์์ฑ (1) | 2023.10.14 |
---|---|
๐ฌMySQL&๐PHP : ๋๊ณ ๋์ ์ฝ๋ผ๋ฆฌ์ ์ํต ์ฐ๊ธฐ์ ์ญ์ (1) | 2023.10.12 |
๐ฌMySQL&๐PHP : ๋๊ณ ๋์ ์ฝ๋ผ๋ฆฌ์ ์ธํ๋ฆฌ ๋ณด์ (0) | 2023.10.09 |
๐ฌMySQL&๐PHP : ๋๊ณ ๋์ ์ฝ๋ผ๋ฆฌ์ ๋ง์ ์ฝ๊ธฐ (0) | 2023.10.06 |
๐ฌMySQL&๐PHP : ๋๊ณ ๋์ ์ฝ๋ผ๋ฆฌ์ ์์ฑ (0) | 2023.10.06 |