html - Insert data from form into MYSQL using Node JS -
i trying inserting data mysql using node , express framework html form. code form :
<html> <head> <title>personal information</title> </head> <body> <div id="info"> <h1>personal information</h1> <form action="/myaction" method="post"> <label for="name">name:</label> <input type="text" id="name" name="name" placeholder="enter full name" /> <br><br> <label for="email">email:</label> <input type="email" id="email" name="email" placeholder="enter email address" /> <br><br> <label for="city">city:</label> <input type="text" id="city" name="city" placeholder="enter city" /> <br><br> <label for="pincode">pincode:</label> <input type="text" id="pincode" name="pincode" placeholder="enter pincode" /> <br><br> <input type="submit" value="send message" /> </form> </div> </body> </html>
and .js file :
var express = require('express'); var app = express(); var ejs = require('ejs'); var mysql = require('mysql'); var bodyparser = require('body-parser'); app.use(bodyparser.urlencoded({ extended: true })); var host = 'localhost'; var port = 3000 var mysql_user = 'root'; var mysql_pass = 'jelly123#'; var database = 'form'; var table = 'info'; var mysql = mysql.createconnection({ host: host, port: port, user: mysql_user, password: mysql_pass, }); app.get('/home',function(req,res,next){ res.sendfile('views/forms.html'); }); app.post('/myaction', function(req, res) { console.log('req.body'); console.log(req.body); res.write('you sent name "' + req.body.name+'".\n'); res.write('you sent email "' + req.body.email+'".\n'); res.write('you sent city "' + req.body.city+'".\n'); res.write('you sent pincode "' + req.body.pincode+'".\n'); res.end() mysql.query("insert "+table+" (name,email,city,pincode) values ('"+req.body.name+"','"+req.body.email+"','"+req.body.city+"','"+req.body.pincode+"')",function(err, result) { if (err) throw err; }); }); app.listen(3000); console.log('example app listening @ port:3000');
i able enter form data , display in page http://localhost:3000/myaction unable insert data database please mention doing wrong.
thanks in advance.
var express = require('express'); var app = express(); var ejs = require('ejs'); var pg = require('pg'); var bodyparser = require('body-parser'); app.use(bodyparser.urlencoded({ extended: true })); var constring = process.env.database_url || "postgres://postgres:emdsystems@localhost:5432/student"; var client = new pg.client(constring); client.connect(); app.get('/',function(req,res,next){ res.sendfile('views/forms.html'); }); app.post('/myaction', function(req, res) { console.log('req.body'); console.log(req.body); res.write('you sent name "' + req.body.name+'".\n'); res.write('you sent email "' + req.body.email+'".\n'); res.write('you sent city "' + req.body.city+'".\n'); res.write('you sent pincode "' + req.body.pincode+'".\n'); res.end() client.query("insert record (name,email,city,pincode) values ('"+req.body.name+"','"+req.body.email+"','"+req.body.city+"','"+req.body.pincode+"')",function(err, result) { if (err) throw err; }); }); app.listen(3000); console.log('example app listening @ port:3000');
Comments
Post a Comment