디비 연결 테스트 ¶
<?php
$link = mysql_connect('mysql5.hosting.paran.com', 'linflus', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}else{
echo("Connect Succesful.<br/>");
}
$db = mysql_select_db("linflus_db");
/*
$sql = "CREATE TABLE guest
(no integer not null,
name char(10),
contents text(2000),
password char(4),
status integer,
date date)";
$result = mysql_query($sql);
if(!$result){
die('Can not create table');
}else{
echo($result);
}
*/
$sql = "INSERT INTO guest
values(1, 'ksh', 'hello', '1234', 1)";
$result = mysql_query($sql);
$sql = "SELECT * FROM guest";
$result = mysql_query($sql);
$obj = mysql_fetch_object($result);
echo($obj->name);
?>
기본 화면 (index.php) ¶
<html> <head><title>방명록</title><head> <body> <form method='post' action='output.php'> 이름 <input type='text' name='input_name' size='10'> 비밀번호 <input type="password" name='input_pw'size='4' maxlength='4'> <br> <textarea name='context' rows='4' cols='50'></textarea> <br> <input type='submit' value=' 전송 '> </form> </body> </html>
글자 수 제한의 처리 (process.php) ¶
<?
//이름 입력의 처리
if (strlen($_POST['input_name']) > 10 )
{
$posted_name = substr($_POST['input_name'],0,10);
}
else
{
$posted_name = $_POST['input_name'];
}
//비번 입력의 처리
if (strlen($_POST['input_pw']) > 4 )
{
$posted_pw = substr($_POST['input_pw'],0,4);
}
else
{
$posted_pw = $_POST['input_pw'];
}
//내용 입력의 처리
if (strlen($_POST['context']) > 2000 )
{
$posted_context = substr($_POST['context'],0,2000);
}
else
{
$posted_context = $_POST['context'];
}
?>
입력 받은 내용 출력 (output.php) ¶
<html><head><title></title></head><body>
<?
include_once("process.php");
echo("입력받은 이름 : ".$_POST['input_name']."<br>");
echo("입력받은 비번 : ".$_POST['input_pw']."<br>");
echo("입력받은 내용 : ".$_POST['context']."<br>");
echo("<br>");
echo("출력된 이름(10byte) : ".$posted_name."<br>");
echo("출력된 비번(04byte) : ".$posted_pw."<br>");
echo("출력된 내용(2000byte) : ".$posted_context."<br>");
?>
</body>
</html>
페이지(page.php) ¶
<?
include_once("data.php");
$scale = 10;//한 페이지당 글 수
//전체 페이지( $total_page ) 계산
if( $total % $scale == 0)
$total_page = floor($total / $scale );
else
$total_page = floor($total / $scale) + 1;
if(!$page) $page = 1;
//페이지에 따라 $start 계산
$start = ($page -1) * $scale;
for( $i = $start; $i < $start + $scale && $i < $total; $i++){
echo $arr[$i]->input_name;
echo $arr[$i]->input_pw;
echo $arr[$i]->context;
echo "<br>";
}
for( $i=1; $i<=$total_page; $i++){
if($page == $i){
echo" <b>[$i]</b>";
}
else{
echo"<a href='page.php?page=$i'>[$i]</a>";
}
}
?>
요건 그냥 데이터(data.php)
<?
class guestBook {
var $input_name;
var $input_pw;
var $context;
}
$total = 12;//전체 글 수
$arr[12] = new guestBook;
$arr[0]->input_name = '강소현';
$arr[0]->input_pw = '1111';
$arr[0]->context = '반가워요 ?';
$arr[1]->input_name = '강승현';
$arr[1]->input_pw = '1122';
$arr[1]->context = '?????';
$arr[2]->input_name = '이정은';
$arr[2]->input_pw = '4444';
$arr[2]->context = '이런!! ㅠㅠㅠㅠ';
$arr[3]->input_name = '초운영';
$arr[3]->input_pw = '5555';
$arr[3]->context = '수강신청 실패 ㅠㅠ';
$arr[4]->input_name = '김상호';
$arr[4]->input_pw = '1242';
$arr[4]->context = '오늘도 지각이에요 ㅠㅠㅋ';
$arr[5]->input_name = '박정근';
$arr[5]->input_pw = '4576';
$arr[5]->context = '안녕하세요??';
$arr[6]->input_name = '컴공인';
$arr[6]->input_pw = '9994';
$arr[6]->context = '여기는 6피';
$arr[7]->input_name = '아아아';
$arr[7]->input_pw = '5221';
$arr[7]->context = '이름이 안 떠올라';
$arr[8]->input_name = '살려줘';
$arr[8]->input_pw = '4524';
$arr[8]->context = '이런!! ㅠㅠ 한계가 왔어!';
$arr[9]->input_name = '언제까지';
$arr[9]->input_pw = '1300';
$arr[9]->context = '이어지니?';
$arr[10]->input_name = '에구구';
$arr[10]->input_pw = '1142';
$arr[10]->context = '길디 길다';
$arr[11]->input_name = '공학관';
$arr[11]->input_pw = '4523';
$arr[11]->context = '입니다 ㅇㅁㅇ';
?>
오늘의 상태 ¶
나머지 부분은 생략
기본화면에 첨부하면 되므로..ㅋ
기본화면에 첨부하면 되므로..ㅋ
<html> <head><title>방명록</title><head> <body> . . <input type = "radio" name = "state" value = "1" size = "40" checked><img src="1.jpg"> <input type = "radio" name = "state" value = "2" size = "40"><img src="2.jpg"> <input type = "radio" name = "state" value = "3" size = "40"><img src="3.jpg"> <input type = "radio" name = "state" value = "4" size = "40"><img src="4.jpg"> . </form> </body> </html>
색 바꾸기 ¶
<?
echo("<h1>배경색을 바꾸자</h1><br>");
echo("
<form method='post' action='$PHP_SELF'>
R <input type='text' maxlength='3' name='setr' size='3'><br>
G <input type='text' maxlength='3' name='setg' size='3'><br>
B <input type='text' maxlength='3' name='setb' size='3'><br>
혹은 그냥 색 이름 입력(영어로) <input type='text' name='clrname'><br>
<input type='submit' value='전송'>
");
if($_POST['clrname'] != NULL)
{
echo('<body bgcolor='.$_POST['clrname'].'>');
}
else
{
// 배경 색의 처리
$hexr=dechex($_POST['setr']);
$hexg=dechex($_POST['setg']);
$hexb=dechex($_POST['setb']);
echo('<body bgcolor='.$hexr.$hexg.$hexb.'>');
}
// 글자가 보이기 위해 글자 색의 처리
$txtr=dechex(255-$_POST['setr']);
$txtg=dechex(255-$_POST['setg']);
$txtb=dechex(255-$_POST['setb']);
?>
메인화면 ( index.php ) ¶
<html> <head> <META charset='utf-8'> <title>방명록</title> <? include "dbconnect.php"; ?> <head> <body> <form method='post' action='index.php'> 이름 <input type='text' name='input_name' maxlength='10' size='10'> 비밀번호 <input type="password" name='input_pw'size='4' maxlength='4'> <br> <textarea name='context' rows='4' cols='50'></textarea> <br> <input type='submit' value=' 전송 '> </form> <? include "make_query.php"; include "process.php"; include "print.php"; include "get_time.php"; ?> </body> </html>
데이터베이스에 연결 ( dbconnect.php ) ¶
<?
$dbconn = mysql_connect("intra.zeropage.org", "csephp", "2010php");
$tableconn = mysql_select_db("csephp_db", $dbconn);
if($dbconn) {
echo("db 연결 성공 <br>");
}
else
{
echo("db 연결 실패 <br>");
}
if($tableconn) {
echo("table 연결 성공 <br>");
}
else
{
echo("table 연결 실패 <br>");
}
echo("<br>"." ------------------------------------------"."<br>");
?>
입력받은 내용을 쿼리로 만들어서 쏜다! ( make_query.php ) ¶
<?
//이름 입력의 처리
if (strlen($_POST['input_name']) > 10 )
{
$posted_name = substr($_POST['input_name'],0,10);
}
else
{
$posted_name = $_POST['input_name'];
}
//비번 입력의 처리
if (strlen($_POST['input_pw']) > 4 )
{
$posted_pw = substr($_POST['input_pw'],0,4);
}
else
{
$posted_pw = $_POST['input_pw'];
}
//내용 입력의 처리
if (strlen($_POST['context']) > 2000 )
{
$posted_context = substr($_POST['context'],0,2000);
}
else
{
$posted_context = $_POST['context'];
}
include "get_time.php";
$query = "insert into guest (name, pw, contents, date) values ('$posted_name', '$posted_pw', '$posted_context', '$date')";
?>
쿼리를 만들때 시간을 저장하기 위해.. ( get_time.php ) ¶
<?
echo('<br>');
$unix_ts=mktime();
$date=date('Y-m-d', $unix_ts);
?>
입력을 받고, 이제 전체 테이블 출력! ( print.php ) ¶
<?
$query = "select * from guest";
$result = mysql_query($query, $dbconn);
$record_number = mysql_num_rows($result);
for($i=0 ; $i < $record_number ; $i ++)
{
include "get_record.php";
echo("기본정보 : $name $no $pw $status $date <br>");
echo("내 용 : $contents <br><br>");
}
?>










