결과물

- index.php에 'change my information' 버튼을 만들었다. 이는 mypage.php로 넘어가게 된다.



- 비밀번호가 다르면 위와 같이 알람창이 나오는 것을 알 수 있다.


- 변경이 완료되면 logout이 자동으로 된다.

- 다시 로그인 하면 아래와 같이 나오는 것을 알 수 있다.
코드
//mypage.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="mypage.js"></script>
</head>
<body>
<?php
session_start();
if(!isset($_SESSION['id'])){
echo "<script>location.replace('login.php');</script>";
}
else{
$id = $_SESSION['id'];
$mysqli = new mysqli('localhost', 'conn', 'Testnote!%89', 'test');
$q = "SELECT id, email, addr from member where id = '$id'";
$result = ($mysqli->query($q))->fetch_array(MYSQLI_ASSOC);
?>
<form action="mypage_update.php" method="post" onsubmit="check()">
<input type="text" name="id" id="id" placeholder="<?php echo $result['id']?>">
<input type="text" name="email" id="email" placeholder="<?php echo $result['email']?>">
<input type="text" name="addr" id="addr" placeholder="<?php echo $result['addr']?>">
<input type="text" name="passwd" id="passwd">
<input type="text" name="check_passwd" id="check_passwd">
<input type="submit" value="수정하기">
</form>
<?php }
?>
</body>
</html>
//mypage.js
function check(){
passwd = document.getElementById('passwd').value;
checkpasswd = document.getElementById('check_passwd').value;
if(passwd != checkpasswd){
alert('비밀번호가 다릅니다!');
return false;
}
return true;
}
- 해당 함수는 패스워드가 일치하는지 확인하고 참이면 true, 거짓이면 false를 리턴한다.
//mypage_update.php
<?php
session_start();
if(!isset($_SESSION['id'])){
echo "<script>location.replace('login.php');</script>";
}
else{
$id = $_SESSION['id'];
$change_id = $_POST['id'];
$email = $_POST['email'];
$addr = $_POST['addr'];
$passwd = $_POST['check_passwd'];
$q = "UPDATE member SET ";
$q1 = " WHERE id = '$id'";
$updateFields = array();
if(!empty($change_id)){
$updateFields[] = "id = '$change_id'";
}
if(!empty($email)){
$updateFields[] = "email = '$email'";
}
if(!empty($addr)){
$updateFields[] = "addr = '$addr'";
}
if(!empty($passwd)){
$updateFields[] = "pw = '$passwd'";
}
if(!empty($updateFields)){
$q .= implode(", ", $updateFields).$q1;
$mysqli = new mysqli('localhost', 'conn', 'Testnote!%89', 'test');
$result = $mysqli->query($q);
if($result){
echo "<script>location.href='logout.php';</script>";
}
else{
echo "<script>alert('문제 발생!');</script>";
echo "<script>location.href='mypage.php';</script>";
}
}
}
?>
- implode를 이용해 update하고자 하는 인자를 추가하고 그 인자가 여러개이면 ','를 붙인다.
수정할 부분
- address를 가져오는 부분이 코드로 구현되어있기 때문에 일일이 작성하는 부분을 빼고 DB에서 주소를 가져오는 코드를 넣을 수 있을 것 같다.
- css로 디자인이 필요한 것 같다.