본문 바로가기

WEB/Nodejs-mysql14

[Nodejs-mysql] Select값 수정하기 [생활코딩] MySQL join을 이용해서 글수정 구현 수정페이지에 Select 넣고 선택되었던 option 출력하기 main.js if(pathname === '/update'){ db.query('SELECT*FROM topic', function(error, topics){ if(error){ throw error; } db.query('SELECT*FROM topic WHERE id=?', [queryData.id], function(error2, topic){ if(error2){ throw error2; } db.query('SELECT*FROM author', function(error3, authors){ // author값 받아서 authors에 담기 var list = template... 2020. 8. 7.
[Nodejs-mysql] 데이터 Select의 option으로 출력하고 선택값 저장 [생활코딩] MySQL join을 이용해서 글생성 구현 데이터 Select의 option으로 만들기 if(pathname === '/create'){ db.query('SELECT*FROM topic', function(error, topics){ db.query('SELECT*FROM author', function(error2, authors){ // author값 가져와서 authors에 담기 var tag = ''; var i = 0; while(i < authors.length){ tag = tag + `${authors[i].name}` i++; } // authors값 반복문으로 option 만들어 tag에 담기 // value값으로 authors[i].id 저장 var title = 'WE.. 2020. 8. 6.
[Nodejs-mysql] Join된 테이블에서 값 가져오기 [생활코딩] MySQL join을 이용해서 상세보기 구현 db.query('SELECT*FROM topic', function(error, topics){ if(error){ throw error;} db.query('SELECT*FROM topic LEFT JOIN author ON topic.author_id = author.id WHERE topic.id=?', [queryData.id], function(error2, topic){ // JOIN LEFT로 topic과 author 데이터 가져오기 // ON으로 두 테이블 매칭시키기 // WHERE에서 어떤 테이블의 id값 가져올지 명시하기 if(error2){throw error2;} var title = topic[0].title; var de.. 2020. 8. 6.
[Nodejs-mysql] 데이터 수정/삭제하기 [생활코딩] MySQL로 글 수정 기능 구현 [생활코딩] MySQL로 글 삭제 기능 구현 수정할 데이터 폼에 출력 if(pathname === '/update'){ db.query('SELECT*FROM topic', function(error, topics){ // topic 데이터 불러와 topics에 담음 if(error){ throw error; } // 예외처리 db.query('SELECT*FROM topic WHERE id=?', [queryData.id], function(error2, topic){ // topic의 특정 id값을 가져와 topic에 담음 // id값은 배열로 가져와 ?에 치환하며, 그 값은 queryData.id if(error2){ throw error2; } // 예.. 2020. 8. 6.
[Nodejs-mysql] 데이터 넣고 출력하기 [생활코딩] MySQL로 글생성 기능 구현 else if(pathname === '/create_process'){ var body = ''; request.on('data', function(data){ body = body + data; }); request.on('end', function(){ var post = qs.parse(body); var title = post.title; var description = post.description; db.query(`INSERT INTO topic (title, description, created, author_id) VALUES (?, ?, NOW(), ?)`, [title, description, 1], function(err, result){.. 2020. 8. 5.
[Nodejs-mysql] 데이터 가져와서 출력하기 [생활코딩] MySQL로 홈페이지 구현 [생활코딩] MySQL로 상세보기 구현 데이터베이스 접속하기 var mysql = require('mysql'); var db = mysql.createConnection({ host : 'localhost', user : 'root', password : 'abc123', database : 'opentutorials' }); db.connect(); topic데이터 가져오기 main.js if(pathname === '/'){ if(queryData.id === undefined){ // 홈페이지 db.query(`SELECT*FROM topic`, function(error, topics){ // 쿼리문을 실행하고 그 결과 값을 topics로 가져옴 var t.. 2020. 8. 5.
반응형