🔓데이터베이스/SQL

메모장 만들기 [primary key 선언하기]

하얀성 2023. 11. 8. 09:40


데이터 테이블 생성


1. 데이터 기입하기

 

http://localhost/after/6/memoshow.php

 

memoshow.php  입력확인코드

//check 함수의 매개변수를 통해 메시지 출력, exit 없으면 함수가 계속 창띄움

<?
	$con =   mysql_connect("localhost", "root", "apmsetup");
	mysql_select_db("comma1",  $con);
	
	$result = mysql_query("select * from memojang2 order by num desc",$con);
	$total = mysql_num_rows($result);
	
	if (!$total) {
		echo ("아직 등록된 글이 없습니다");
} 	else {
		echo ("
		<table border=1 width=700>
		<tr><td width=100>이름</td><td width=150>날짜</td><td width=450>메모</td></tr>
		");
		
		$counter = 0;
		
		while ($counter < $total) :
			$wname = mysql_result($result, $counter, "name");
			$wdate = mysql_result($result, $counter, "wdate");
			$wmemo = mysql_result($result, $counter, "message");
			
			echo ("<tr><td>$wname</td><td>$wdate</td> <td>$wmemo</td></tr>");
			$counter = $counter + 1;
		endwhile;
		
		echo("</table>");
	}
	
	echo("<br><a href=memo.html>메모쓰기</a>");
	mysql_close($con);
?>

 


memo.php

<?

	
	function check($message) {
		echo("
			<script>
			window.alert(\"$message\");
			history.go(-1);
			</script>
		");
		exit; 
	}
	
	if (!$wname) check("이름을 입력하세요");
	if (!$wmemo) check("내용을 입력하세요");
	
	$con = mysql_connect("localhost", "root", "apmsetup");
	mysql_select_db("comma1", $con);

	$wdate =   date("Y-m-d H:i:s");
	
	mysql_query("insert into memojang2(name, wdate, message) values('$wname','$wdate','$wmemo')",$con);
	
?>

 

memo.html (input을 만들어서 정보를 받도록 한곳)

<html>
<head>
<title> 메모장</title>
</head>
<body>
<form action=memo.php method=post>
<font  size=2>이름</font>
<input type=text size=10 name=wname>
<font  size=2>메모</font>
<input type=text size=30 maxlength=100 name=wmemo>
<input type=submit value="글올리기">
</form>
</body>
</html>

 

action 속성은 폼 데이터가 전송될 서버의 URL을 지정

 

양식을 제출할 때, 데이터는 action에 지정된 URL로 "보내질" 곳이며

 

이때 사용되는 HTTP 메소드(method)는 method 속성에 지정

. method 속성은 보통 GET 또는 POST 방식을 사용하며, 각각의 방식에 따라 데이터를 서버로 전송하는 방식이 달라짐

 

 


페이지 원하는 만큼 끊어서 여러페이지 만들어주기

예상결과

memoshow3.php

 

이유도 없이 url에 cpage에 코드가 전해지지 않아서 애먹었다. 그냥 원래 되던 코드랑 비교해보니 다른게... 내가 일부러 바꿔준것말곤 없다고 해서 난감했다. 새파일 다시 만들어서 했다.

<?

$con =   mysql_connect("localhost", "root", "apmsetup");
mysql_select_db("comma1",   $con);

$result =   mysql_query("select * from memojang2 order by num desc", $con);

$total =   mysql_num_rows($result);

if (!$total)   {
	echo ("아직 등록된 글이 없습니다");
} else {
	echo ("
		<table border=1 width=700>
		<tr><td width=50>글번호</td><td width=100>이름</td>
		<td width=150>쓴날짜</td><td width=400>메모</td></tr>
	");

	$pagesize =   5;	// 한 페이지에 보여 줄 메모 글의 개수

	if ($cpage ==  '') $cpage = 1; 

	$endpage = (int)($total / $pagesize);

	if ( ($total % $pagesize) != 0 ) $endpage = $endpage + 1;

	$i = 0;

	while ( $i   < $pagesize ) :
		$counter = $pagesize * ($cpage - 1) + $i;
		if ($counter == $total) break;
		$num = mysql_result($result, $counter, "num");
		$name = mysql_result($result, $counter, "name");
		$wdate = mysql_result($result, $counter, "wdate");
		$message = mysql_result($result,   $counter, "message");

		echo ("
			<tr><td>$num</td><td>$name</td>
			<td>$wdate</td><td>$message</td></tr>
		");

		$i++;

	endwhile;

	echo ("</table>");
}

echo ("<table border=0 width=700><tr><td align=center>");
	
$ppage =   $cpage - 1;
$npage =   $cpage + 1;

if ($cpage > 1) echo ("[<a href=memoshow3.php?cpage=$ppage>이전페이지</a>]");

if ($cpage < $endpage) echo ("[<a href=memoshow3.php?cpage=$npage>다음페이지</a>]");

echo ("</td></tr>
        <tr><td align=center><a   href=memo.html>메모쓰기</a></td></tr>
   </table>");

mysql_close($con);

?>