3

I have just made a simple program to display and insert data from a database(sql server 2008). My code does the display of data. I am unable to get data inserted. It shows no error in terminal or browser.

Here is my javascriptfile
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/htm', function (req, res) {
res.sendFile( __dirname + "/" + "index.html" );
})
var sql = require("mssql");
var config = {
    user: 'pkp',
    password: 'pkp',
    server: 'PRAVEEN\\SQLEXPRESS', 
    database: 'myneww' 
 };
app.get('/process_get', function (req, res) {

   // Prepare output in JSON format
   response = {
   first_name:req.query.first_name,
   last_name:req.query.last_name
   };
 sql.connect(config, function (err) {
    if (err) console.log(err);
    var request = new sql.Request();
    console.log(req.query.first_name);
    var res=request.query('insert into Mytab values(req.query.first_name ,req.query.last_name)');
    });
});

app.get('/alldata', function (req, res) {   
 sql.connect(config, function (err) {   
    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query('select * from Mytab', function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

    });
});
});
 var server = app.listen(8081, function () {
  var host = server.address().address
  var port = server.address().port
  console.log("Example app listening at http://%s:%s", host, port)
})

Here is my html file

<html>
<body>
<form action="http://127.0.0.1:8081/process_get" method="GET">
First Name: <input type="text" name="first_name">  <br>
Last Name: <input type="text" name="last_name">
<input type="submit" value="Submit">
</form>
</body>
</html>

I am able to get the values displayed in the console, means values are passed and retrieved from the form. But still not inserted into the database.

1
  • ` "insert into dabname(name,city) values(' "+name+" ',' "+city+" ')" ` That's it Commented Dec 16, 2017 at 5:42

2 Answers 2

1

I'm not good in javascript, but I guess the line below is incorrect:

var res=request.query('insert into Mytab values(req.query.first_name ,req.query.last_name)');

It should be something like this.

var res=request.query('insert into Mytab values(' + req.query.first_name + ',' + req.query.last_name +')');

If not, you've got an idea.

Sign up to request clarification or add additional context in comments.

Comments

0

First, you were not passing values properly to query and, secondly, you are not waiting for the record to insert. Add the callback that I added.

app.get('/process_get', function (req, res) {
   //some code
   sql.connect(config, function (err) {
      if (err) console.log(err);
      var request = new sql.Request();
      console.log(req.query.first_name);
      request.query('insert into Mytab values('+req.query.first_name+','+req.query.last_name+')', function(err, recordset) {
           if (err) {
              console.log(err);
              return res.send('Error occured');
           }
           return res.send('Successfully inserted');
       });
   });
});

Update

Use transaction to commit changes.

app.get('/process_get', function (req, res) {
   //some code
   var sqlConn = new sql.Connection(config);
   sqlConn.connect().then(function () {
      var transaction = new sql.Transaction(sqlConn);
      transaction.begin().then(function () {
         var request = new sql.Request(transaction);
         request.query('Insert into EmployeeInfo (firstName,secondName) values ('+req.query.first_name+','+req.query.last_name+')').then(function () {
            transaction.commit().then(function (recordSet) {
                console.log(recordSet);
                sqlConn.close();
                return res.send('Inserted successfully');
            }).catch(function (err) {
                console.log("Error in Transaction Commit " + err);
                sqlConn.close();
                return res.send('Error');
            });
        });
    });
});

Forgive me, if there is any typo.

6 Comments

Hi, I tries as shown, no data inserted into table.. It shows an error res.send is not a function in terminal
try the updated code. earlier answer had typing error.
its shows successfully inserted in browser(but not). In terminal shows some connection error: connection is closed. But still when I give /alldata, I am getting the resultz..
can you share exact error stack trace ? Also, how many columns do you have in Mytab ? insert into Mytab (firstName, lastName) values('parveen', 'krishna')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.