본문 바로가기

Linux/기타

db 활용

============================================================
제로보드에서 최근 게시물을 표시하는 방법

<?php
   $_zb_url = "제로보드가 설치된 url";
   $_zb_path = "제로보드가 설치된 절대경로";
   include $_zb_path."outlogin.php";
?>
	
1. 최근 게시물 목록 출력하기
최근 게시물을 출력하고자 하는 곳에 다음과 같이 입력한다.
<? 
print_bbs("스킨이름", "타이틀", "게시판이름", 목록갯수, 글자제한)
?>

- DocumentRoot 디렉토리 : 웹페이지가 제공되는 시스템의 디렉토리
# cd /usr/local/apache/htdocs
# vi index.html
-- index.html --
<?php
   $_zb_url = "http://192.168.56.230/bbs/";
   $_zb_path = "/usr/local/apache/htdocs/bbs/";
   include $_zb_path."outlogin.php";
?>
<html>
 :
 :
<body>
   :
   :
<? 
print_bbs("default/default_bbs", "보안관련", "security", 10, 30);
print_bbs("default/default_bbs", "리눅스관련", "linux", 10, 30);
print_bbs("default/default_bbs", "나의생활", "life", 10, 30);
?>

</body>
</html>
-- index.html --

!!! 가입 폼 !!!

-- input.php --
<?
/* ... */
//
?>
<!--  input.php -> inputok.php (DB에 저장) -->
<html>
 <head>
  <title> ::: 입력폼 테스트 ::: </title>
 </head>

<body>
  <form method=post action=inputok.php>
  이름 : <input type=text name=name> <br>
  이메일 : <input type=text name=email> <br>
  전화번호 : <input type=text name=cp> <br>
   <input type=submit value=전송>
  </form>

</body>

</html>

-- input.php --

-- inputok.php --
<?
 // GET -> $_GET['변수명']
 // POST -> $_POST['변수명']
 echo "이름 : $_POST[name] <br>";
 echo "이메일 : $_POST[email] <br>";
 echo "전화번호 : $_POST[cp] <br>";

?>
-- inputok.php --



-- table1.php --
<!-- 테이블 모양
+-----+------+
|  1  |  2   |
+-----+------+
|  3  |  4   |
+-----+------+
-->
<html>
  <head>
   <title> ::: 테이블 만들기 연습 ::: </title>
  </head>

<body>
 <table border=0 bgcolor=#000000 cellspacing=1 
        cellpadding=0 height=100 width=100>
  <tr align=center bgcolor=#ffffff>
   <td> 1 </td>  <td> 2 </td>
  </tr>
  <tr align=center bgcolor=#ffffff>
   <td> 3 </td>  <td> 4 </td>
  </tr>
 </table>

  <br>

 <table border=0 bgcolor=#000000 cellspacing=1 
        cellpadding=0 height=100 width=100>
  <tr align=center bgcolor=#ffffff>
   <td colspan=2> 1 </td>
  </tr>
  <tr align=center bgcolor=#ffffff>
   <td> 2 </td>  <td> 3 </td>
  </tr>
 </table>

<br>
<table border=0 bgcolor=#000000 cellspacing=1 
        cellpadding=0 height=100 width=100>
  <tr align=center bgcolor=#ffffff>
   <td rowspan=2> 1 </td> <td > 2 </td>
  </tr>
  <tr align=center bgcolor=#ffffff>
    <td> 3 </td>
  </tr>
 </table>

</body>
</html>

-- table1.php --

-- input.php --
<!--  input.php -> inputok.php (DB에 저장) -->
<html>
 <head>
  <title> ::: 입력폼 테스트 ::: </title>
 </head>

<body>
  <form method=post action=inputok.php>
  <table align=center cellpadding=3 cellspacing=1 bgcolor=black width=300>
   <tr bgcolor=white>
    <td align=center> 이름 </td>  
    <td> <input type=text name=name> </td>
   </tr>
   <tr bgcolor=white>
    <td align=center> 이메일 </td> 
    <td> <input type=text name=email> </td>
   </tr>
   <tr bgcolor=white>
    <td align=center> 전화번호 </td> 
    <td> <input type=text name=cp> </td>
   </tr>
   <tr bgcolor=black>
     <td align=center colspan=2> <input type=submit value=전송> </td>
   </tr>
  </table>
  </form>

<?
 // DB의 연결 정보를 불러온다.
 include "dbconnect.php";

 // 쿼리문을 생성한다.
 // no 를 내림차순으로 정렬해서 검색한다.
 $query = "SELECT * FROM userinfo ORDER BY no DESC";

 $res = mysql_query($query);
 $count = mysql_num_rows($res);  // 전체 자료의 수를 구한다.
 // print_r($row);
?>
 
 <table align=center cellpadding=3 cellspacing=1 bgcolor=black width=500>
   <tr bgcolor=white>
    <td align=center> 번호 </td>  
    <td align=center> 이름  </td>  
    <td align=center> 이메일  </td>  
    <td align=center> 전화번호  </td>  
    <td align=center> 삭제  </td>  
   </tr>

  <?
    while($count)
    {
      $row = mysql_fetch_array($res); // 현재 행의 자료를 구한다.
   ?>
       <tr bgcolor=white>
        <td align=center> <?=$count ?> </td>  
        <td align=center> <?=$row[name]?> </td>  
        <td align=center> <?=$row[email]?>  </td>  
        <td align=center> <?=$row[cellphone]?>  </td>  
        <td align=center> <a href=del.php?no=<?=$row[no]?>>삭제</a>  </td>  

       </tr>
  <?
      $count--;
    }
  ?>

   </table>

 // 화면에 DB 내용을 출력한다.


</body>

</html>

-- input.php --

-- dbconnect.php --
<?
 $dbhost = "localhost";
 $dbuser = "bbsuser";
 $dbpass = "bbsuser1234";
 $dbname = "bbsuser";
 $tbname = "userinfo";

 $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("DB 접속에 실패했습니다. 관리자에게 문의하세요");
 mysql_select_db($dbname);
?>
-- dbconnect.php --

-- inputok.php --
<?
 // GET -> $_GET['변수명']
 // POST -> $_POST['변수명']
 // mysql_connect("호스트명","사용자명", "비밀번호"); 
 // mysql_select_db("DB명");
 // mysql_query("쿼리);
 // print_r($_POST);

 // DB의 연결 정보를 불러온다.
 include "dbconnect.php";

 // 쿼리문을 생성한다.
 $query = "INSERT into $tbname VALUES(
            '', 
            '$_POST[name]', 
            '$_POST[email]', 
            '$_POST[cp]',
            '$_SERVER[REMOTE_ADDR]', 
            now())";

 // echo $query;
 // table 에 사용자가 입력한 정보를 저장한다.
 mysql_query($query);

 // table 에 입력하고 /input.php 로 이동시킨다.
 echo "<script language=JavaScript>
       <!--
           alert('사용자가 정상적으로 저장되었습니다');
           location.href = '/input.php';
       -->
       </script>
      ";


?>
-- inputok.php --

- inputok.php -> del.php(삭제확인) -> delok.php(완전삭제)
    no=3  --------> no=3 --------------> no=3
-- del.php --
<?
 // print_r($_GET);
 include "dbconnect.php";
 $sel_query = "SELECT * FROM $tbname WHERE no = $_GET[no]";
 // echo $sel_query;

 $res = mysql_query($sel_query);
 $row = mysql_fetch_array($res);
?>

<script language=Javascript>
<!--
  function goback()
  {
    alert("삭제를 취소했습니다");
    location.href = "/input.php";
  }

  function delok(no)
  {
    // alert(no);
    // alert("자료를 삭제합니다");
    // c : strcat, php : . javascript : +
    page = "delok.php?no=" + no ;
    // alert(page);
    location.href = page;
  }
-->
</script>

 <table align=center cellpadding=3 cellspacing=1 bgcolor=black width=500>
   <tr bgcolor=white>
    <td align=center> 번호 </td>  
    <td align=center> 이름  </td>  
    <td align=center> 이메일  </td>  
    <td align=center> 전화번호  </td>  
   </tr>
   <tr bgcolor=white>
    <td align=center> <?=$row[no]?> </td>  
    <td align=center> <?echo $row[name]; ?>  </td>  
    <td align=center> <?=$row[email]?>  </td>  
    <td align=center> <?=$row[cellphone]?>  </td>  
   </tr>
   <tr bgcolor=white>
    <td align=center colspan=4>
      <input type=button value=삭제하기 onClick="delok(<?=$row[no]?>)">
      <input type=button value=취소하기 onClick="goback()">
    </td>
   </tr>

  </table>


-- del.php --

-- delok.php --
<?
  // echo "no = " . $_GET[no];
  include "dbconnect.php";
  $del_query = "DELETE FROM $tbname WHERE no = $_GET[no]";
  mysql_query($del_query);
  echo "<script language=JavaScript>
        <!--
	  alert( $_GET[no] + ' 자료를 삭제했습니다!!!');
	  location.href = 'input.php';
	-->
	</script>
       ";

?>
-- delok.php --

- 명령행에서 mysql의 -e 옵션 이용해서 테이블의 내용을 볼 수 있다.
- dbname.tablename
# mysql -e "select * from bbsuser.userinfo"

-- 테이블 생성 --
# mysql bbsuser
mysql> \e
 create table userinfo (       
 no        int default 0 auto_increment,
 name      varchar(20),
 email     varchar(60),
 cellphone varchar(15),
 ipaddress varchar(15),
 date      datetime,
 primary key (no)
 );

mysql> desc userinfo;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| no        | int(11)     |      | PRI | NULL    | auto_increment |
| name      | varchar(20) | YES  |     | NULL    |                |
| email     | varchar(60) | YES  |     | NULL    |                |
| cellphone | varchar(15) | YES  |     | NULL    |                |
| ipaddress | varchar(15) | YES  |     | NULL    |                |
| date      | datetime    | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

-- 테이블 생성 --



============================================================


- table 에서 값을 삭제할 때 아래 쿼리문을 사용한다.
delete from userinfo ;  <-- userinfo 테이블 내용 모두 삭제
delete from userinfo where no='번호';  <-- 번호만 삭제
delete from userinfo where no <='3';  <-- 번호만 삭제
delete from userinfo where no='3' and no = '5';  <-- 번호만 삭제

V 블럭을 잡는다.
d 스택에 정보를 집어넣는다.
:new dbconnect.php
p 붙히기를 한다.


'Linux > 기타' 카테고리의 다른 글

네임서버 세팅하기  (0) 2015.06.11
연습 2  (0) 2015.05.29
어셈블리[if]  (0) 2015.05.26
ebp  (0) 2015.05.19
HDD추가하기  (0) 2015.04.29