http://ynsoft.co.kr/y1 | 작업게시판 | 로그인 | 회원가입 | 장바구니(0) | 주문조회 | 온라인견적 | FAQ | 마이페이지 
 
    
    
자동로그인
쇼핑몰
IT
와이앤소프트업무
학원교육
프로젝트
개인의
전례음악
공부

Node.js
 
작성일 : 24-03-21 22:27
[일반] html 에서 post 값을 받아서 mysql 에 저장하기
 글쓴이 : 가우텔
조회 : 56  
[animal 테이블 생성]
create table animal (
  id int unsigned not null auto_increment primary key,
  name varchar(255) not null,
  age int(3) not null,
  sound varchar(255) not null
);

[index.html]
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>동물 데이터 입력</title>
</head>
<body>

    <h3>동물 데이터 입력</h3>
    <form method="post" action="//localhost:3000">
        <input type="text" name="name" placeholder="이름을 입력하세요.">
        <input type="text" name="age" placeholder="나이를 입력하세요.">
        <input type="text" name="sound" placeholder="소리를 입력하세요.">
        <br>
        <button type="submit">제출하기</button>
    </form>
   
</body>
</html>


[server.js]
const express = require('express')
const mysql = require('mysql2');
const app = express()
const port = 3000

// 데이터베이스와 연결
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456q!',
    database: 'mytest'
  });

app.use(express.urlencoded({ extended: true }));

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

app.post('/',function(req,res){
    // console.log(req.body.name)

    let name = req.body.name;
    let age = req.body.age;
    let sound = req.body.sound;

    // post 로 넘어온 변수를 mysql에 입력
    connection.query(
        'insert into animal set name=?, age=?, sound=?',
        [name, age, sound],
        function(err) {
            if (err)
                throw err;
            console.log('입력')
        }
    )
})


 
 

Total 8
번호 제   목 글쓴이 날짜 조회
8 [일반] crud 기초 예제 가우텔 05-09 68
7 [일반] html에서 post로 값 넘기고 express로 값을 받아서… 가우텔 04-05 53
6 [일반] html 에서 post 값을 받아서 mysql 에 저장하기 가우텔 03-21 57
5 [일반] file directory 생성, 수정, 삭제, 목록보기 가우텔 03-08 54
4 [일반] mysql2 사용하기 가우텔 03-07 58
3 [일반] mysql 사용법 가우텔 03-07 57
2 [일반] express 이용 get post 받기 가우텔 03-07 58
1 관련사이트 가우텔 04-03 59
회사소개 | 서비스이용약관 | 개인정보 취급방침
OO도 OO시 OO구 OO동 123-45 / 전화 : 02-123-4567 / 팩스 : 02-123-4568 / 운영자 : 가우텔
사업자 등록번호 : 123-45-67890 / 대표 : 대표자명 / 개인정보관리책임자 : 정보책임자명
통신판매업신고번호 : 제 OO구 - 123호 / 부가통신사업신고번호 : 12345호
Copyright © 2001-2005 회사명. All Rights Reserved.