<?php
include "db2.php";
$sql="SELECT * FROM board WHERE idx = '$_GET[idx]'";
$result=mysqli_query($conn,$sql);
// var_dump($result);
$row=mysqli_fetch_array($result);
$sql2="UPDATE board SET hit = '$row[hit]'+1 WHERE idx=$_GET[idx]";
$result2=mysqli_query($conn, $sql2);
// session_start();
// var_dump($_SESSION['id']);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="" method="POST">
<input type="hidden" name="idx" value="<?=$_GET['idx']?>">
<table width="500" border="1">
<tr>
<td colspan="6" >Detail hit<?=$row['hit'] + 1?></td>
</tr>
<tr>
<th>Title</th>
<td><input type="text" name="title" value=<?=$row['title']?>></td>
</tr>
<tr>
<th>Content</th>
<td><textarea name="content"><?=$row['content']?></textarea></td>
</tr>
<tr>
<th>PassWord</th>
<td><input type="password" name="password" placeholder="PassWord"></td>
</tr>
<tr>
<td colspan="6">
<input type="submit" value="Modify" onclick="return confirm('really')" formaction="modify_p.php">
<input type="submit" value="Delete" onclick="return confirm('really')" formaction="delete.php">
<input type="submit" value="Back" formaction="index.php">
</td>
</tr>
</table>
</form>
</body>
</html>
modify_p.php
<?php
session_start();
include "db2.php";
$idx=$_POST['idx'];
$sql="SELECT * FROM board WHERE idx = '$idx'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
if($_SESSION['admin'] === "admin" || $_SESSION['name'] === $row['name'] && $_COOKIE['normal']==="ok"){
if(isset($_POST['title']) && isset($_POST['content']) && isset($_POST['password'])){
if(empty($_POST['title'])){
echo "<script>
alert(\"EmptyTitle\");
history.back(1);
</script>";
exit();
}elseif(empty($_POST['content'])){
echo "<script>
alert(\"EmptyContent\");
history.back(1);
</script>";
exit();
}elseif(empty($_POST['password'])){
echo "<script>
alert(\"EmptyPw\");
history.back(1);
</script>";
exit();
}
$idx=$_POST['idx'];
$title=$_POST['title'];
$content=$_POST['content'];
$password=$_POST['password'];
$title=mysqli_escape_string($conn,$title);
$password=mysqli_escape_string($conn,$password);
$content=mysqli_escape_string($conn,$content);
if($password === $row['pwd']){
$sql2="UPDATE board SET
title = '$title',
content = '$content'
WHERE idx = '$idx'";
$result2=mysqli_query($conn,$sql2);
if($result2){
header("Location: index.php");
}else{
echo "<script>
alert(\"Fail\");
history.back(1);
</script>";
exit();
}
}else{
echo "<script>
alert(\"IncorrectPW\");
history.back(1);
</script>";
exit();
}
}else{
echo "<script>
alert(\"Only Contact manager\");
history.back(1);
</script>";
exit();
}
}else{
echo "<script>
alert(\"Only Contact manager\");
history.back(1);
</script>";
exit();
}
// echo $_POST['idx'];
?>
1.include "db2.php";은 $conn부분이 계속 반복되기 때문에 따로 페이지를 만들어 의미없는 반복을 줄일려고 했다.
db2.php
<?php
define('db_p','localhost');
define('db_id','root');
define('db_pw','123456789a');
define('db_t','test');
$conn=mysqli_connect(db_p,db_id,db_pw,db_t);
?>
2.$sql="SELECT * FROM board WHERE idx = '$_GET[idx]'";과
$result=mysqli_query($conn,$sql);부분은 <textarea name="content"><?=$row['content']?></textarea>같은 $row부분에 쓰기 위해 사용했다. 왜냐면 input text 태그에 value를 값으로 넣어 수정에 용이하게 만들었다.

3.<input type="password" name="password" placeholder="PassWord">를 넣은 이유는 modify_p.php에 있는 if($password === $row['pwd']) 코드를 이용하여 Create페이지에서 글을 쓸때 DB에 저장한 password가 일치하면 글을 수정할 수 하기 위해 만들었다.
4.$sql2="UPDATE board SET title = '$title', content = '$content' WHERE idx = '$idx'";은 DB에 정보를 수정하기 위한 코드이며 UPDATE (table) SET (DB에 저장된 row) = '(바꿀정보)' WHERE(조건) 식별가능한 열(in DB) = '데이터' 이다.
5.$sql2="UPDATE board SET hit = '$row[hit]'+1 WHERE idx=$_GET[idx]";는 이 페이지에 접속할때 마다 숫자 1을 더하게해서 조회수를 늘려주는 코드이다.
---Normaltic 4주차 4일---