How to post data using node in mysql .


      This   blog   will  discuss  insert  data    from   html page    into  mysql. Following  steps  we need


                   1.  Create  database  in mysql .

                   2.  Create  a table .

                   3. Create  html page.

                   4. create  connection js . 

          1.    Create  database . 

             mysql >  Type  this query   to  create database . 

                  create     database  mkjhadatabase  

     2.     Then   use  this  database query  to create   login table. 

               CREATE TABLE login (

       name varchar(255),
       password varchar(255),

you need to create a folder mylogin   and change the directory .

 3.  Create  a  html page . 



<div class="login-form"><!--login form-->
<h2>Login to your account</h2>
<form action="http://localhost:4000/login" method="POST">
 <label for="name">Name:</label>

<input type="text" id="name" name="name" placeholder="Enter your full name" />
        <label for="password">Password:</label><input type="password" id="email" name="password" placeholder="Enter your password " />
             <br><br> <button type="submit" class="btn btn-default">Login</button>
</div><!--/login form-->




4. Creat  a  file  login.js.

 var express = require("express");
var app     = express();
const router = express.Router();

var path    = require("path");
var mysql = require('mysql');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
var con = mysql.createConnection({
 host: "localhost",
  user: "root",
  password: "tiger123",
  database: "mkjhadatabase"


     var  name     =;
      var  password= req.body.password;
     var sql = `INSERT INTO  login (name, password  ) VALUES ('${name}', '${password}' )`;

  res.write('You sent the name "' +'".\n');
  res.write('You sent the Email "' + req.body.password+'".\n');

   console.log("User dat is inserted successfully "); 
   con.connect(function(err) {
      if (err) throw err;
     con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("1 record inserted");

       console.log(" data   inserted successfully "); 


console.log("Running at Port 4000");

Now  go  to  terminal . 

 change  the directory  mylogin where login.html and node.js file is stored.

        > node  login.js. 


Now    go  the  browser  and    type  the   http://localhost:4000/login  . 

  login.html   form  will  appear . 

How to post data using node in mysql .  How  to post  data using  node  in mysql . Reviewed by Mukesh Jha on 6:41 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.