1 / 34

MySQL 연동 PHP 프로그래밍 기초

MySQL 연동 PHP 프로그래밍 기초. 기본적인 HTML 태그. HTML 소스. <html> <title>MySQL+PHP 연동 프로그램입니다 .</title> <body> <center><h1> 학생 리스트 </h1> <table border=2 bgcolor=white> <tr> <td> 학번 </td><td> 이름 </td><td> 학과 </td><td> 전화번호 </td> </tr> <tr> <td>S006</td> <td> 황정숙 </td> <td> 사학과 </td>

samuru
Télécharger la présentation

MySQL 연동 PHP 프로그래밍 기초

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MySQL 연동 PHP 프로그래밍 기초

  2. 기본적인 HTML 태그

  3. HTML 소스 <html> <title>MySQL+PHP 연동 프로그램입니다.</title> <body> <center><h1>학생 리스트</h1> <table border=2 bgcolor=white> <tr> <td>학번</td><td>이름</td><td>학과</td><td>전화번호</td> </tr> <tr> <td>S006</td> <td>황정숙</td> <td>사학과</td> <td>678-9012</td> </tr> <tr> <td>S005</td> <td>김상진</td>

  4. HTML 소스 <td>사학과</td> <td>567-8901</td> </tr> <tr> <td>S002</td> <td>최민국</td> <td>컴퓨터공학과</td> <td>234-5678</td> </tr> <tr> <td>S001</td> <td>박소명</td> <td>컴퓨터공학과</td> <td>123-4567</td> </tr> </table> </center> </body> </html>

  5. HTML 태그

  6. MySQL 관련 PHP 함수

  7. MySQL 연동 PHP 프로그램 구성 • MySQL 서버와 연결 설정 • 데이터베이스 선택 • SQL 실행 • SQL 수행결과 레코드 추출, 컬럼 추출 • 버퍼 해제 • MySQL 서버와 연결 종료

  8. PHP와 MySQL 연동 함수 • MySQL 연결 • 연결변수 = mysql_connect(호스트, 사용자이름, 패스워드); • $db = mysql_connect(“localhost”,“root”,“1234”); • 데이터베이스 선택 • mysql_select_db(데이터베이스, 연결변수); • mysql_select_db(“education”, $db); • SQL 실행 • 결과변수 = mysql_query(SQL 명령, 연결변수); • $result = mysql_query(“select * from student”, $db);

  9. PHP와 MySQL 연동 함수 • SQL 수행결과 레코드 추출, 컬럼 추출 • mysql_result(결과변수,인덱스,필드명); • mysql_result($result, $i, scode); • 레코드변수 = mysql_fetch_array(결과변수); • $row = mysql_fetch_array($result); • 레코드변수[필드이름] 또는 레코드변수[인덱스] • $row[scode] 또는 $row[0] • 버퍼해제 • mysql_free_result(결과변수); • 연결종료 • mysql_close(연결변수);

  10. 학생 테이블 출력 프로그램 <html> <title>MySQL+PHP 연동 프로그램입니다.</title> <body> <?php // 화일명 : 학생 리스트를 출력하는 liststudent.php echo "<center><h1>학생 리스트</h1>\n"; $conn = mysql_connect('localhost','root',‘1234') or die("MySQL 서버 연결 에러"); mysql_select_db('education',$conn) or die("DB 오픈 에러"); $result = mysql_query("select * from student",$conn); echo "<table border=2 bgcolor=white>\n"; echo "<tr>\n"; echo "<td>학번</td><td>이름</td><td>학과</td><td>전화번호</td>\n"; echo "</tr>\n"; $rows = mysql_num_rows($result); // 전체 레코드의 수 반환 $i = 0;

  11. 학생 테이블 출력 프로그램 while ( $i < $rows ) { echo "<tr>\n"; echo "\t<td>" . mysql_result($result, $i, scode) . "</td>\n"; echo "\t<td>" . mysql_result($result, $i, sname) . "</td>\n"; echo "\t<td>" . mysql_result($result, $i, sdept) . "</td>\n"; echo "\t<td>" . mysql_result($result, $i, sphone) . "</td>\n"; echo "</tr>\n"; $i++; } echo "</table>\n"; echo "</center>\n"; mysql_free_result($result); mysql_close($conn); ?> </body> </html>

  12. 폼 입력 값을 통한 데이터 검색

  13. FORM 태그 • HTML에서 사용자 입력을 받는 태그 <FORM NAME=“폼이름”METHOD=[POST|GET] ACTION=“실행문서”> <INPUT TYPE=[TEXT|PASSWORD…] NAME=“변수명”VALUE=“초기값”> <INPUT TYPE=[SUBMIT|RESET] VALUE=“버튼이름”> </FORM> • METHOD • 사용자 입력을 웹서버에 전달하는 방식을 지정 • POST 방식은 웹 브라우저의 주소입력 부분에 입력 값이 표시되지 않는다. • GET 방식은 입력값이 표시

  14. FORM 태그 • ACTION • SUBMIT 버튼 클릭 시 실행되는 웹 서버 상의 프로그램 지정 • INPUT 태그 • 입력변수의 데이터 타입, 크기 등과 같은 속성 지정 • 문자열 입력 시 TYPE 속성은 “TEXT” • 암호 입력 시 TYPE 속성은 “PASSWORD” • NAME 속성으로 입력변수 지정

  15. select.html <html> <title>폼 입력 예제......</title> <body> <center> <h3>검색하려는 학생 이름을 입력하세요</h3> <form name="PHP" method=post action="select.php"> 학생이름 : <input type=text name="fname"><br><br> <input type=submit value="검색"> <input type=reset value="취소"> </form> </center> </body> </html>

  16. select.php <html> <title> 학생 검색 </title> <body> <center> <?php // 화일명 : select.php echo "입력된 폼 변수값 : " . $fname . "<br><br>\n"; $conn = mysql_connect('localhost',‘root',‘1234'); mysql_select_db('education',$conn); $result = mysql_query("select * from student where sname = '$fname'",$conn); echo "검색된 결과 ===><br><br>\n";

  17. select.php while ( $row = mysql_fetch_array($result) ) { echo "학번: " . $row[scode] . "<br>\n"; echo "이름: " . $row[sname] . "<br>\n"; echo "학과: " . $row[sdept] . "<br>\n"; echo "전화: " . $row[sphone] . "<br>\n"; } mysql_free_result($result); mysql_close($conn); ?> </center> </body> </html>

  18. 데이터 입력 처리 프로그램

  19. insert-st.html <html> <meta http-equiv="Content-Type" content="text/html; charset=euc-kr"> <title>학생 레코드 입력...</title> <body> <center> <h3>학생 카드 입력</h3> <form name="PHP" method=post action="insert-st.php"> 학번 : <input type=text name="scode"><br> 이름 : <input type=text name="sname"><br> 학과 : <input type=text name="sdept"><br> 전화 : <input type=text name="sphone"><br><br> <input type=submit value="저장"> <input type=reset value="취소"> </form> </center> </body> </html>

  20. insert-st.php <html> <title> 학생 레코드 저장 </title> <body> <center> <?php // 화일명 : insert-st.php $conn = mysql_connect('localhost',‘root',‘1234'); mysql_select_db('education',$conn); $sql = "insert into student (scode, sname, sdept, sphone) "; $sql = $sql . " values ('$scode', '$sname', '$sdept', '$sphone')"; $result = mysql_query($sql,$conn); $result = mysql_query("select * from student where sname = '$sname'",$conn);

  21. insert-st.php echo "저장된 학생 레코드 ===><br><br>\n"; while ( $row = mysql_fetch_array($result) ) { echo "학번: " . $row[scode] . "<br>\n"; echo "이름: " . $row[sname] . "<br>\n"; echo "학과: " . $row[sdept] . "<br>\n"; echo "전화: " . $row[sphone] . "<br>\n"; } mysql_free_result($result); mysql_close($conn); ?> </center> </body> </html>

  22. 데이터 수정 처리 프로그램

  23. 데이터 수정 절차 • 검색된 레코드의 변경할 필드를 입력받아 수정 • 수정절차 • 변경하려는 레코드가 테이블에 있는지 확인 • 기존의 필드들을 출력 • 데이터 수정을 위해 새로운 필드값 입력 • 입력받은 필드값으로 테이블 수정 • update-st.php • Form 태그와 php 액션 프로그램을 같은 파일=> $PHP_SELF 이용 • INPUT 태그의 hidden 속성, readonly 속성 사용

  24. 수정처리 화면 1

  25. 수정처리 화면 2

  26. update-st.php <html> <title> 학생 레코드 수정 </title> <body> <center> <font size=+1><b>학생 카드 검색 및 수정</b></font> <form name="search" method=post action="<?php echo $PHP_SELF; ?>"> 이름 : <input type=text name="sname" value="<?php echo $sname;?>"><br> <input type=hidden name=mode value='search'><br> <input type=submit value="검색"> <input type=reset value="취소"> </form> <hr><br> <?php // 화일명 : update-st.php

  27. update-st.php if ( $sname ) { $conn = mysql_connect('localhost','prof','phpmysql'); mysql_select_db('education',$conn); if ( $mode == 'update' ) { $sql = "update student set scode='$scode', sname='$sname',"; $sql = $sql . " sdept='$sdept', sphone='$sphone' where scode='$scode'"; $result = mysql_query($sql,$conn); } $result = mysql_query("select * from student where sname = '$sname'",$conn); $row = mysql_fetch_array($result); echo "<font size=+1><b>학생 카드 수정</b></font>\n"; echo "<form name=update method=post action=$PHP_SELF>\n"; echo "학번 : <input type=text name=scode "; echo " readonly value=$row[scode]><br>\n";

  28. update-st.php echo "이름 : <input type=text name=sname value=$row[sname]><br>\n"; echo "학과 : <input type=text name=sdept value=$row[sdept]><br>\n"; echo "전화 : <input type=text name=sphone value=$row[sphone]><br><br>\n"; echo "<input type=hidden name=mode value='update'>\n"; echo "<input type=submit value='수정'>&nbsp;"; echo "<input type=reset value='취소'>\n"; echo "</form>"; echo "<hr><br>\n"; mysql_free_result($result); mysql_close($conn); $sname = ""; } ?> </center> </body> </html>

  29. 데이터 삭제 처리 프로그램

  30. 데이터 삭제 절차 • 레코드의 이름을 입력받아 삭제 • 수정절차 • 삭제하려는 레코드가 테이블에 있는지 확인 • 기존의 필드들을 출력 • 데이터 삭제 • delete-st.php • 앞의 데이터 수정과 유사 • SQL로 DELETE 문장 사용

  31. 삭제 화면

  32. delete-st.php <html> <title> 학생 레코드 삭제 </title> <body> <center> <font size=+1><b>학생 카드 검색 및 삭제</b></font> <form name="search" method=post action="<?php echo $PHP_SELF; ?>"> 이름 : <input type=text name="sname" value="<?php echo $sname;?>"><br> <input type=hidden name=mode value='search'><br> <input type=submit value="검색"> <input type=reset value="취소"> </form> <hr><br> <?php // 화일명 : delete-st.php

  33. delete-st.php if ( $sname ) { $conn = mysql_connect('localhost','prof','phpmysql'); mysql_select_db('education',$conn); if ( $mode == 'delete' ) { $sql = "delete from student where scode = '$scode'"; $result = mysql_query($sql,$conn); } $result = mysql_query("select * from student where sname = '$sname'",$conn); $row = mysql_fetch_array($result); echo "<font size=+1><b>학생 카드 삭제</b></font>\n"; echo "<form name=delete method=post action=$PHP_SELF>\n"; echo "학번 : <input type=text name=scode readonly value=$row[scode]><br>\n"; echo "이름 : <input type=text name=sname readonly value=$row[sname]><br>\n"; echo "학과 : <input type=text name=sdept readonly value=$row[sdept]><br>\n"; echo "전화 : <input type=text name=sphone readonly value=$row[sphone]><br><br>\n";

  34. delete-st.php echo "<input type=hidden name=mode value='delete'>\n"; echo "<input type=submit value='삭제확인'>&nbsp;"; echo "<input type=reset value='취소'>\n"; echo "</form>"; echo "<hr><br>\n"; mysql_free_result($result); mysql_close($conn); $sname = ""; } ?> </center> </body> </html>

More Related