How to insert form data in to mysql using express ejs

3.1k Views Asked by At

I am new to Node.JS, so I created a form in express ejs in views/pages/profile.ejs as follow:

<form role="form" action="/profile" method="post">
  <div class="box-body">
    <h4>Generl Details</h4>
    <div class="input-group">
      <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
      <input type="email" class="form-control" placeholder="Email" name="email_id">
    </div>
    <br/>
    <div class="input-group">
       <span class="input-group-addon"><i class="fa fa-user"></i></span>
       <input type="text" class="form-control" placeholder="First Name" name="first_name">
    </div>
    <br/>
    <div class="input-group">
       <span class="input-group-addon"><i class="fa fa-user"></i></span>
       <input type="text" class="form-control" placeholder="Last Name" name="last_name">
    </div>
    <div class="form-group">
        <label>Address</label>
        <textarea class="form-control" rows="3" name="address" placeholder="Enter you Address ..."></textarea>
      </div>
      <div class="form-group">
        <label>Gender</label>
        <select class="form-control">
          <option>Male</option>
          <option>Female</option>
        </select>
      </div>
  </div><!--/.col (left) -->
  <!-- right column -->
  <div class="box-footer">
    <input type="submit" class="btn btn-primary" id="profilesubmit" value="submit">
  </div>
</form>

I want to insert this form in the MySql database on submit.

I have create a dbquery/index.js for all DB queries. This file looks like:

// load up the user model
var mysql = require('mysql');
var bcrypt = require('bcrypt-nodejs');
var dbconfig = require('../config/database');
var connection = mysql.createConnection(dbconfig.connection);
connection.query('USE ' + dbconfig.database);
var express  = require('express');
var app      = express();
var bodyParser = require('body-parser')
/*app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); */
app.use(express.bodyParser()); // get information from html forms
module.exports = function(dbquery) {
app.post("/profile", function(req, res){
    connection.query("SELECT * FROM users WHERE username = ?",[username], function(err, rows) {
        if (err)
            return done(err);
        if (rows.length) {
            return done(null, false, req.flash('signupMessage', 'That username is already taken.'));
        } else {
            // if there is no user with that username
            // create the user
            console.log(req.body.first_name);
            var newUserMysql = {
                email_id : req.body.email_id,
                first_name : req.body.first_name,  // use the generateHash function in our user model
                last_name : req.body.last_name
            };

            var insertQuery = "INSERT INTO users ( email_id, first_name, last_name ) values (?,?)";

            res.send(connection.query(insertQuery,[newUserMysql.email_id, newUserMysql.first_name, newUserMysql.last_name],function(err, rows) {
                        newUserMysql.id = rows.insertId;

                        return done(null, newUserMysql);
                    }));
        }
    });
});
};

This all I have done for inserting data in to MySQL. Nothing is added into table. Also let me know if any other details needed. Any help on this will be great. Thank you...

0

There are 0 best solutions below