종합실습예제
:간단한 쇼핑몰 DB설계 및 DB에 데이터를 입출력하는 웹페이지 제작

 

 

1) 데이터베이스 생성 및 MySQL사용자 생성/권한 부여


- DB이름: shopDB

  USE mysql;
  CREATE DATABASE shopDB;

 


2) MySQL 사용자: shopuser
 

- 암호: shop1234

  USE shopDB;
  GRANT ALL ON shopDB.* TO
shopuser@localhost identified by ‘shop1234’ ;
  flush privileges;

 


3) 테이블 디자인 및 생성
 

- 회원 테이블 구성
    테이블이름: member

Field

Type

Null

Constraint

Extra

Description 

   m_id

   int

   NO

  PRIMARY KEY

 auto_increment

  회원ID

   nick

   char(10)

   NO

  UNIQUE

 -

  별명

   addr

   char(20)

   YES

  -

 -

  주소

   phone

   char(11)

   NO

  -

 -

  전화번호

   job

   char(15)

   YES

  -

 -

  직업

   point

   int

   YES

  -

 -

  적립포인트

   email

   varchar(30)

   NO

  UNIQUE

 -

  이메일주소


 

   USE shopDB;
  CREATE TABLE member

  (     m_id INT PRIMARY KEY AUTO_INCREMENT,

        nick CHAR(10) UNIQUE NOT NULL,

        addr CHAR(20),

        phone CHAR(11) NOT NULL,

        job CHAR(15),

        point INT,

        email VARCHAR(30) NOT NULL UNIQUE );

 

 

 

- 카테고리 테이블 구성

    테이블 이름 : category

  Field    Type    Null  Constraint  Extra  Description 
  cat_id     int    NO  PRIMARY KEY  auto_increment   카테고리번호
  cat_name    varchar(20)    NO  UNIQUE  -   카테고리이름
  cat_desc    varchar(40)    YES    -   카테고리설명

 

 

  USE shopDB ;

  CREATE TABLE category

  ( cat_id INT PRIMARY KEY AUTO_INCREMENT,

    cat_name VARCHAR(20) UNIQUE NOT NULL,

    cat_desc VARCHAR(40) );

 


 

- 제품 테이블 구성

    테이블 이름 : product

  Field   Type   Null  Constraint   Extra  Description 
  p_id   int   NO  PRIMARY KEY  auto_increment  제품ID
  cat_id   int   YES

 FOREIGN KEY

 :category(cat_id)

 -  카테고리ID
  descript   varchar(100)   YES  -  -  제품설명
  price   int   NO  -  -  가격
  p_num   int   NO  -  -  재고수량

 

 

  USE shopDB;

  CREATE TABLE product

  ( p_id INT PRIMARY KEY AUTO_INCREMENT,

    cat_id INT REFERENCES category(cat_id),

    descript VARCHAR(100),

    price INT NOT NULL,

    p_num INT NOT NULL);

 

 

 

- 구매 테이블 구성

    테이블 이름 : buyTable

  Field   Type     Null   Constraint   Extra  Description 
  b_id   int   NO  PRIMARY KEY  auto_increment   주문번호
  p_id   int   NO

 FOREIGN KEY

 : product(p_id)

 -   제품ID
  m_id   int   NO

 FOREIGN KEY

 : member(m_id)

 -   회원ID
  b_num   int   NO  -  -   주문수량
  b_date   datetime   NO  -  -   주문일시

 

 

  USE shopDB;

  CREATE TABLE buyTable

  ( b_id INT PRIMARY KEY AUTO_INCREMENT,

    p_id INT REFERENCES product(p_id),

    m_id INT REFERENCES member(m_id),

    b_num INT NOT NULL,

    b_date DATETIME NOT NULL );

 

 

 

4) 신규 카테고리 항목 입력  웹페이지 작성


vi /usr/local/apache/htdocs/cat_insert.html

 

  <html>

  <head> <title>카테고리 항목 입력</title> </head>

  <body> <form action="cat_insert.php" method="POST">

  <p>카테고리 이름:<br>

  <input type="text" name="cat_name" size="30">

  <p>카테고리 설명:<br>

  <input type="text" name="cat_desc" size="30">

  <p><input type="submit" name="submit" value="입력"></p>

  </form> </body>

  </html>

 

 

 

vi /usr/local/apache/htdocs/cat_insert.php

 

  <?php

  $mysqli = mysqli_connect("localhost","shop_user","shop1234","shopDB");

  if (mysqli_connect_errno()) {

             printf("Connect failed: %s\n",mysqli_connect_error());

             exit();

  }

  else {

            $sql = "insert into category (cat_name,cat_desc)

            values ('".$_POST["cat_name"]."','".$_POST["cat_desc"]."')" ;

            $res = mysqli_query($mysqli,$sql);

            if ($res === TRUE) {

                          echo "A record has been inserted.";

            }

            else {

                          printf("Could not insert record: %s\n",mysqli_error($mysqli));

            }

  mysqli_close($mysqli);

  }

  ?>

 

 

 

5) 입력된 레코드 출력 php파일 작성


vi /usr/local/apache/htdocs/cat_fetch.php

 

  <?php

  $mysqli = mysqli_connect("localhost","shop_user","shop1234","shopDB");

  if (mysqli_connect_errno()) {

             printf("Connect failed: %s\n",mysqli_connect_error());

             exit();

  }

  else {

             $sql = "select * from category";

             $res = mysqli_query($mysqli,$sql);

             if ($res) {

                           while ($newArray = mysqli_fetch_array($res,MYSQLI_ASSOC)) {

                                        $cat_name = $newArray['cat_name'];

                                        $cat_desc= $newArray['cat_desc'];

                                        echo "The ID is ".$cat_name." and the text is ".$cat_desc."<br/>";

                                        }

             }

            else {

                           printf("Could not retrieve records: %s\n",mysqli_error($mysqli));

             }

             mysqli_free_result($res);

             mysqli_close($mysqli);

  }

  ?>

 


 





 

 

Posted by 으랏차
,