728x90
- 웹 서버 : Apache
- 데이터베이스 : MySQL, MariaDB
- 프로그래밍 언어 : PHP, HTML
통합 소프트웨어 XAMPP(Apache+MariaDB+PHP+Perl)
1. DB만들기
<?php
$con=mysqli_connect("localhost", "root", "1234", "") or die("MySQL 접속 실패!!");
$sql="create database sqldb";
$ret = mysqli_query($con, $sql);
if($ret) {
echo "sqldb가 성공적으로 생성됨.";
}
else {
echo "sqldb 생성 실패!!!", "<br>";
echo "실패 원인 :", mysqli_error($con);
}
mysqli_close($con);
?>
2. Table만들기
<?php
$con=mysqli_connect("localhost", "root", "1234", "sqldb") or die("MySQL 접속 실패!!");
$sql = "
create table usertbl
( userid char(8) not null primary key,
name varchar(10) not null,
birthyear int not null,
addr char(2) not null,
mobile1 char(3),
mobile2 char(8),
height smallint,
mdate date
)
";
$ret = mysqli_query($con, $sql);
if($ret) {
echo "usertbl을 성공적으로 생성함!!";
}
else {
echo "usertbl 생성 실패!", "<br>";
echo "실패 원인 :", mysqli_error($con);
}
mysqli_close($con)
?>
3. 페이지 만들기(1) - 초기 화면
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<h1> 회원 관리 시스템 </h1>
<a href='select.php'> (1) 회원 조회 (조회 후 수정/삭제 가능) </a> <br><br>
<a href='insert.php'> (2) 신규 회원 등록 </a> <br><br>
<form method="get" action="update.php">
(3) 회원 수정 - 회원 아이디 : <input type = "text" name="userid">
<input type="submit" value="수정">
</form>
<form method="get" action="delete.php">
(4) 회원 삭제 - 회원 아이디 : <input type = "text" name="userid">
<input type="submit" value="삭제">
</form>
</body>
</html>
4. 페이지 만들기(2) - 회원 조회
<?php
$con=mysqli_connect("localhost", "root", "1234", "sqldb") or die("MySQL 접속 실패!!");
$sql = "select * from usertbl";
$ret = mysqli_query($con, $sql);
if($ret) {
$count = mysqli_num_rows($ret);
}
else {
echo "usertbl 데이터 조회 실패!!!", "<br>";
echo "실패 원인 :", mysqli_error($con);
exit();
}
echo "<h1> 회원 조회 결과 </h1>";
echo "<table border=1>";
echo "<TR>";
echo "<TH>아이디</TH><TH>이름</TH><TH>출생년도</TH><TH>지역</TH><TH>극번</TH>";
echo "<TH>전화번호</TH><TH>키</TH><TH>가입일</TH><TH>수정</TH><TH>삭제</TH>";
echo "</TR>";
while($row = mysqli_fetch_array($ret)) {
echo "<TR>";
echo "<TD>", $row['userid'], "</TD>";
echo "<TD>", $row['name'], "</TD>";
echo "<TD>", $row['birthyear'], "</TD>";
echo "<TD>", $row['addr'], "</TD>";
echo "<TD>", $row['mobile1'], "</TD>";
echo "<TD>", $row['mobile2'], "</TD>";
echo "<TD>", $row['height'], "</TD>";
echo "<TD>", $row['mdate'], "</TD>";
echo "<TD>", "<a href='update.php?userid=", $row['userid'], "'>수정</a></TD>";
echo "<TD>", "<a href='delete.php?userid=", $row['userid'], "'>삭제</a></TD>";
echo "</TR>";
}
mysqli_close($con);
echo "</table>";
echo "<br> <a href='main.html'> <--초기 화면</a> ";
?>
5. 페이지 만들기(3) - 회원 입력
- 입력 받을 페이지 만들기
-
<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> <h1> 신규 회원 입력 </h1> <form method="post" action="insert_result.php"> 아이디 : <input type = "text" name="userid"> <br> 이름 : <input type = "text" name="name"> <br> 출생년도 : <input type = "text" name="birthyear"> <br> 지역 : <input type = "text" name="addr"> <br> 휴대폰 국번 : <input type = "text" name="mobile1"> <br> 휴대폰 전화번호 : <input type = "text" name="mobile2"> <br> 신장 : <input type = "text" name="height"> <br> <br><br> <input type="submit" value="회원 입력"> </form> </body> </html>
-
- 입력 받은 정보 데이터베이스로 넘겨주기
-
<?php $con=mysqli_connect("localhost", "root", "1234", "sqldb") or die("MySQL 접속 실패!!"); $userid = $_POST["userid"]; $name = $_POST["name"]; $birthyear = $_POST["birthyear"]; $addr = $_POST["addr"]; $mobile1 = $_POST["mobile1"]; $mobile2 = $_POST["mobile2"]; $height = $_POST["height"]; $mdate = $_POST["mdate"]; $sql = "insert into usertbl values('".$userid."','".$name."',".$birthyear; $sql = $sql.",'".$addr."', '".$mobile1."', '".$mobile2."', '".$height."', '".$mdate."')"; $ret = mysqli_query($con, $sql); echo "<h1> 신규 회원 입력 결과 </h1>"; if($ret) { echo "데이터가 성공적으로 입력됨."; } else { echo "데이터 입력 실패!!!"."<br>"; echo "실패 원인 : ".mysqli_error($con); } mysqli_close($con); echo "<br> <a href='main.html'> <--초기 화면</a> "; ?>
-
6. 페이지 만들기(4) - 회원 수정
<?php
$con=mysqli_connect("localhost", "root", "1234", "sqldb") or die("MySQL 접속 실패!!");
$sql = "select * from usertbl where userid='".$_GET['userid']."'";
$ret = mysqli_query($con, $sql);
if($ret) {
$count = mysqli_num_rows($ret);
if ($count==0) {
echo $_GET['userid']." 아이디의 회원이 없음!!"."<br>";
echo "<br> <a href='main.html'> <--초기 화면</a> ";
exit();
}
}
else {
echo "데이터 조회 실패!!!"."<br>";
echo "실패 원인 : ".mysqli_error($con);
echo "<br> <a href='main.html'> <--초기 화면</a>";
exit();
}
$row = mysqli_fetch_array($ret);
$userid = $row['userid'];
$name = $row['name'];
$birthyear = $row['birthyear'];
$addr = $row['addr'];
$mobile1 = $row['mobile1'];
$mobile2 = $row['mobile2'];
$height = $row['height'];
$mdate = $row['mdate'];
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<h1> 회원 정보 수정 </h1>
<form method="post" action="update_result.php">
아이디 : <input type = "text" name="userid" value=<?php echo $userid ?> readonly> <br><br>
이름 : <input type = "text" name="name" value=<?php echo $name ?>> <br><br>
출생년도 : <input type = "text" name="birthyear" value=<?php echo $birthyear ?>> <br><br>
지역 : <input type = "text" name="addr" value=<?php echo $addr ?>> <br><br>
휴대폰 국번 : <input type = "text" name="mobile1" value=<?php echo $mobile1 ?>> <br><br>
휴대폰 전화번호 : <input type = "text" name="mobile2" value=<?php echo $mobile2 ?>> <br><br>
신장 : <input type = "text" name="height" value=<?php echo $height ?>> <br><br>
가입일 : <input type = "text" name="mdate" value=<?php echo $mdate ?> readonly> <br><br>
<br><br>
<input type="submit" value="정보 수정">
</form>
</body>
</html>
<?php
$con=mysqli_connect("localhost", "root", "1234", "sqldb") or die("MySQL 접속 실패!!");
$userid = $_POST["userid"];
$name = $_POST["name"];
$birthyear = $_POST["birthyear"];
$addr = $_POST["addr"];
$mobile1 = $_POST["mobile1"];
$mobile2 = $_POST["mobile2"];
$height = $_POST["height"];
$mdate = $_POST["mdate"];
$sql = "update usertbl set name ='".$name."', birthyear=".$birthyear;
$sql = $sql.", addr = '".$addr."', mobile1 = '".$mobile1."', mobile2 = '".$mobile2;
$sql = $sql.", height = '".$height."', mdate = '".$mdate."' where userid = '".$userid."'";
$ret = mysqli_query($con, $sql);
echo "<h1> 회원 정보 수정 결과 </h1>";
if($ret) {
echo "데이터가 성공적으로 수정됨.";
}
else {
echo "데이터 수정 실패!!!"."<br>";
echo "실패 원인 : ".mysqli_error($con);
}
mysqli_close($con);
echo "<br> <a href='main.html'> <--초기 화면</a> ";
?>
7. 페이지 만들기(5) - 회원 삭제
<?php
$con=mysqli_connect("localhost", "root", "1234", "sqldb") or die("MySQL 접속 실패!!");
$sql = "select * from usertbl where userid='".$_GET['userid']."'";
$ret = mysqli_query($con, $sql);
if($ret) {
$count = mysqli_num_rows($ret);
if ($count==0) {
echo $_GET['userid']." 아이디의 회원이 없음!!"."<br>";
echo "<br> <a href='main.html'> <--초기 화면</a> ";
exit();
}
}
else {
echo "데이터 조회 실패!!!"."<br>";
echo "실패 원인 : ".mysqli_error($con);
echo "<br> <a href='main.html'> <--초기 화면</a>";
exit();
}
$row = mysqli_fetch_array($ret);
$userid = $row['userid'];
$name = $row['name'];
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<h1> 회원 삭제 </h1>
<form method="post" action="delete_result.php">
아이디 : <input type = "text" name="userid" value=<?php echo $userid ?> readonly> <br><br>
이름 : <input type = "text" name="name" value=<?php echo $name ?> readonly> <br><br>
<br><br>
위 회원을 삭제하겠습니까?
<input type="submit" value="정보 삭제">
</form>
</body>
</html>
<?php
$con=mysqli_connect("localhost", "root", "1234", "sqldb") or die("MySQL 접속 실패!!");
$userid = $_POST["userid"];
$sql = "delete from usertbl where userid = '".$userid."'";
$ret = mysqli_query($con, $sql);
echo "<h1> 회원 정보 삭제 결과 </h1>";
if($ret) {
echo "데이터가 성공적으로 삭제됨.";
}
else {
echo "데이터 삭제 실패!!!"."<br>";
echo "실패 원인 : ".mysqli_error($con);
}
mysqli_close($con);
echo "<br> <a href='main.html'> <--초기 화면</a> ";
?>
'SQL' 카테고리의 다른 글
DBMS 개요 (0) | 2021.09.01 |
---|---|
SQL 용어 정리 (0) | 2021.09.01 |
MySQL - SQL 고급 (0) | 2021.08.23 |
MySQL - SQL 기본(2) (0) | 2021.08.23 |
MySQL - SQL 기본(1) (0) | 2021.08.20 |