Friday 27 April 2018

help with using node.js and express to submit info to sql db

Hi, I'm working on a project where I need to make an html document that will submit entered info to my web api.Currently, I have my api working, heres the code:const mysql = require('mysql'); const express = require('express'); const app = express(); var con = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "classnew" }); //you may get an error here, try changing database: "classnew" to something else. I'm using "classnew" because when I want to view my pokemon.sql in cmd prompt, I type database classnew; and source pokemon.sql; var home = app.get('/', (req, res) => res.send('Welcome to my Pokemon API. We are using only gen 1 pokemon here! Use localhost:3000/pokemon to view your pokedex!')); var pageOne = app.get('/pokemon', function(req, res){ con.connect(function(err) { if (err) throw err; console.log("Pokedex Connected!"); con.query("SELECT * FROM pokemon", function (err, result) { res.json(result); if (err) throw err; //console.log(result); }); }); }); var info = app.get('/pokemon/:id', function(req, res){ var id = req.params.id; con.query("SELECT * FROM pokemon WHERE id = " + id, function (err, result) { res.json(result); if (err) throw err; console.log(result); }); }); app.listen(3000, () => console.log('Example app listening on port 3000!')) and here is my HTML document that I need to use to submit new info to my SQL database:

Add a new pokemon here. Please separate multiple inputs with a comma. If null, please use '-'.

Enter the name of new pokemon:



Enter its type:



Enter what it is immune to:



Enter what it strongly resists:



Enter what it resists:



Enter what it is weak to:



Enter what it is very weak to:



Enter its evolution name:



I'm sorry that the project is on pokemon, atleast I'm only doing gen 1 pokemon. ;PAny way, i need to be able to open my forms.html, fill in the form boxes, click submit and have it INSERT INTO my sql document (will paste my sql db at the send), and also be able to use my API to see that newly submitted info.I DO NOT WANT CODE SNIPPETS, I WANT TO LEARN AND UNDERSTAND HOW THIS WORKS, SO ANY RESOURCES WOULD BE MUCH MORE BENEFICIAL TO ME THAN A COPY PASTA. THANK YOU.heres my sqldb: CREATE TABLE pokemon (id INTEGER PRIMARY KEY, name TEXT, type TEXT, immuneTo TEXT, strongResist TEXT, resits TEXT, weakTo TEXT, veryWeakTo TEXT, evolutionName TEXT); /* id | name | type | immuneTo | strongResist | resists | weakTo | veryWeakTo | evolutionName | */ INSERT INTO pokemon VALUES (1, "Pikachu", "Electric", NULL, NULL, "Electric, Flying", "Ground", NULL, "Raichu"); INSERT INTO pokemon VALUES (2, "Haunter", "Ghost, Poison", "Fighting, Normal", "Poison", "Grass", "Bug, Ghost, Ground, Psychic", NULL, "Gengar"); INSERT INTO pokemon VALUES (3, "Kadabra", "Psychic", "Ghost", NULL, "Fighting, Psychic", "Bug", NULL, "Alakazam"); INSERT INTO pokemon VALUES (4, "Snorlax", "Normal", "Ghost", NULL, NULL, "Fighting", NULL, NULL); INSERT INTO pokemon VALUES (5, "Diglett", "Ground", "Electric", NULL, "Poison, Rock", "Grass, Ice, Water", NULL, "Dugtrio"); INSERT INTO pokemon VALUES (6, "Geodude", "Ground, Rock", "Electric", "Poison", "Fire, Flying, Normal, Rock", "Fighting, Ground, Ice", "Grass, Water", "Graveler"); SELECT * FROM pokemon;

Submitted April 27, 2018 at 05:37PM by extDASH

No comments:

Post a Comment