레이블이 예제인 게시물을 표시합니다. 모든 게시물 표시
레이블이 예제인 게시물을 표시합니다. 모든 게시물 표시

2010년 3월 15일 월요일

간단한 PHP+AJAX 예제

모든 파일은 utf-8로 작성되었습니다.

index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
    <title> new document </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="example.js"></script>
</head>
<body>
<div id="head"> hello, guest!</div>
<input type="text" id="name" value="" onkeyup="change()"/>
</body>
</html>



example.js
//XmlHttpRequest 객체 생성
if (typeof ActiveXObject != 'undefined')
{
    XMLHttpRequest = function ()
    {
        return new ActiveXObject(navigator.userAgent.indexOf('MSIE 5') >-1 ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');
    };
}
var xhr = new XMLHttpRequest();

// 객체 생성하고 함수 생성
function change()
{
    var name = document.getElementById("name").value;
    var url = "exam.php?name="+name;
    xhr.open("Get",url,true);
    xhr.onreadystatechange = callback;
    xhr.send();
}
function callback()
{
    if (xhr.readyState == 4)
    {
        if (xhr.status == 200)
        {
            var txt = xhr.responseText;
            document.getElementById('head').innerHTML = txt;
        }
    }
}


exam.php :
<?php
header("Content-Type: text/html; charset=UTF-8");
$name = $_REQUEST["name"];
echo "welcome ".$name."!!";
?>


[이미지 출처 : http://compstat.chonbuk.ac.kr/JavaScript/]
참고사이트 http://compstat.chonbuk.ac.kr/JavaScript/

2009년 5월 27일 수요일

[MYSQL] 펌 - Foreign key 예제



 

MySQL foreign key 예제


a_tbl, b_tbl 두개의 테이블이 있다고 하자



 create table a_tbl(

 user_id varchar(10) not null default'',

 user_name varchar(30) not null default'',

 primary key(`user_id`)

 ) engine=innodb;





 create table b_tbl(

 user_id varchar(10) not null,

 user_tel varchar(15) not null,

 primary key(`user_id`),

 constraint fk_user_id       -- 제약조건명 : fk_user_id

 foreign key (`user_id`)     -- 테이블의 필드(user_id)가 foreign key

 references a_tbl(`user_id`) -- 테이블(a_tbl)의 필드(user_id)를 참조 

 on delete cascade           -- 참조키가 삭제되면 foreign key도 삭제

 on update cascade           -- 참조키가 업데이트되면 foreign key도 업데이트

 ) engine=innodb;


 CASCADE : 참조키가 삭제되거나 업데이트 되면 동일하게 foreign key도 동일하게 맞춤.

 RESTRICT : 참조키 변경(삭제 혹은 업데이트)되는 것을 방지함.

 SET NULL : 참조키에 변경(삭제 혹은 업데이트) 되면 foreign key 값을 null 로 저장.

 NO ACTION : 참조키에 변경(삭제 혹은 업데이트) 되어도 아무런 동작을 하지 않음.(foreign key의 이미 없음)


 각 테이블에 데이터 입력를 입력하자.



 insert into a_tbl values('happy','hong gil dong');

 insert into a_tbl values('appleman','lee soon shin');

 insert into a_tbl values('keeper','lee myoung park');


+----------+-----------------+

| user_id  | user_name       |

+----------+-----------------+

| appleman | lee soon shin   |

| happy    | hong gil dong   |

| keeper   | lee myoung park |

+----------+-----------------+

3 rows in set (0.00 sec)





 insert into b_tbl values('happy','010-1234-5678');

 insert into b_tbl values('appleman','011-4321-8765');

 insert into b_tbl values('keeper','016-9876-5432');


+----------+---------------+

| user_id  | user_tel      |

+----------+---------------+

| appleman | 011-4321-8765 |

| happy    | 010-1234-5678 |

| keeper   | 016-9876-5432 |

+----------+---------------+

3 rows in set (0.00 sec)




 각 두테이블을 조인하자.


 mysql> select a.user_id, b.user_id, a.user_name, b.user_tel from a_tbl a join b_tbl b where a.user_id=b.user_id;

+----------+----------+-----------------+---------------+

| user_id  | user_id  | user_name       | user_tel      |

+----------+----------+-----------------+---------------+

| appleman | appleman | lee soon shin   | 011-4321-8765 |

| happy    | happy    | hong gil dong   | 010-1234-5678 |

| keeper   | keeper   | lee myoung park | 016-9876-5432 |

+----------+----------+-----------------+---------------+


참조키를 업데이트한 후에 확인하자.



 mysql> update a_tbl set user_id='sadman' where user_id='happy';

 Query OK, 1 row affected (0.00 sec)

 Rows matched: 1  Changed: 1  Warnings: 0


mysql> select a.user_id, b.user_id, a.user_name, b.user_tel from a_tbl a join b_tbl b where a.user_id=b.user_id;

+----------+----------+-----------------+---------------+

| user_id  | user_id  | user_name       | user_tel      |

+----------+----------+-----------------+---------------+

| appleman | appleman | lee soon shin   | 011-4321-8765 |

| keeper   | keeper   | lee myoung park | 016-9876-5432 |

| sadman   | sadman   | hong gil dong   | 010-1234-5678 |

+----------+----------+-----------------+---------------+

3 rows in set (0.00 sec)


= 출처 =

운영자 : 김두형(www.sunmysql.co.kr)

email : beansoft@nate.com

업데이트 : 20080923