2016-06-21 83 views
1

我試圖從html頁面通過nodejs向MySQL插入值。但錯誤發生爲「Rollno未定義」。我可以獲取html頁面,但是在提交發生值錯誤時。使用Node.js插入數據

var express=require("express"); 
var app=express(); 
var sql=require('mysql'); 

var con=sql.createConnection({...}); 

app.get('/index',function(req,res){ 
    res.sendFile('index.html',{'root': __dirname }); 
}) 

app.get('/insert',function (req,res){ 

    var data = { 
     Rollno:req.body.Rollno, 
     Name:req.body.Name 
    }; 


    con.query("insert into test set ?",data, function (err,rows){ 
     if(err) throw err; 
     res.send("Value has bee inserted"); 
    }) 

}) 

HTML代碼頁.... 指數 用戶名:密碼 :

回答

2

2建議:

1.啓用體的解析器:

app.use(express.bodyParser()); 

2.Make它作爲POST請求:

app.post('/insert',....) 
+0

謝謝您的回覆拉吉,最終其工作在app.use一個小變化(bodyParser()); – chozhan

+0

@chozhan很高興爲您提供幫助..有一個快樂的編碼..如果它真的幫助你,請接受答案.. – Subburaj