Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Execute Multiple Insert Queries in NodeJs Application?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 846
    Answer it

    I am trying to perform an insert query, use the insertid of the row and then perform another insert query using the inert id. the first query is executing but the second query is not. please help. Below is my code:
    $("#addopdfrm").submit(function(){
    var opdname = $("#opdname").val();
    var opdsex = $("#opdsex").val();
    var opdage = $("#opdage").val();
    var opdaddress = $("#opdaddress").val();
    var opdcity = $("#opdcity").val();
    var opdstate = $("#opdstate").val();
    var opdpin = $("#opdpin").val();
    var opdcon = $("#opdcon").val();
    var opdmail = $("#opdmail").val();
    var opdpresfee = $("#opdpresfee").val();
    var odate = new Date();
    var opdmonth = odate.getMonth()+1;
    var opdyear = odate.getFullYear();
    var opdday = odate.getDate();
    var opddate = opdday+"/"+opdmonth+"/"+opdyear;
    var opdtime = Date.now();
    var opdvisstatus = "Waiting";
    var opdchargedes = "Prescription Fee";
    var opdsyncstat = "no";
    var opdid = "";
    var mysql = require('mysql');
    var connection = mysql.createConnection({
          host: 'localhost',
          user: 'root',
          password: '',
          database: 'plasmadoc',
          multipleStatements: true
          });
          connection.connect(function(err){
        
        if(err){
            return console.log(err.stack);
        }
         });
         var postopd = {opdid:opdid, name: opdname, age: opdage, sex: opdsex, contact: opdcon, address: opdaddress, pin: opdpin, city: opdcity, state: opdstate, email: opdmail, date: opddate, syncstatus: opdsyncstat, opdtime: opdtime};
    connection.query("INSERT INTO `opdrec` SET?", postopd, function(err, rows) {
                    if(err){
                        console.log("An error ocurred performing the query.");
                        console.log(err);
                        return;
                    }                
                    var insertid = rows.insertId;
                    var postvisit = {opdid: insertid, date: opddate, chargedes: opdchargedes, charge: opdpresfee, visitstatus: opdvisstatus, syncstatus: opdsyncstat, vistime: opdtime};
                    connection.query("INSERT INTO `opdvisits` SET?", postvisit, function(error, result){
    if(error){
    console.log(error);
    }
    alert("New OPD Record Added");
    connection.end();
    });
                    });
         
    });
     

 1 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: