2014-05-10 167 views
0

我得到SQL處理錯誤sql:underfined here.Here我檢查登錄表單是否與數據庫匹配,稱爲學生,如果有匹配,然後彈出一個警告彈出歡迎消息和轉移到下一頁。我如何修復代碼?用戶登錄使用科爾多瓦

document.addEventListener("deviceready", onDeviceReady, false); 

      var db; 


      function onDeviceReady() { 

       db = window.openDatabase("Database", "1.0", "Student",2*1024*1024); 
       db.transaction(createDB, errorCB, successCB); 

      } 
function loginForm(){ 
       db.transaction(checkDB, errorCB); 
       $.mobile.changePage("#page5",{reverse:false,transition:"slide"}); 
       return false; 
      } 

     function checkDB(tx){ 
     var _matric=$("[name='matric']").val(); 
     var _password=$("[name='password']").val(); 
     var sql ='select * from STUDENT where matric='+_matric+' and password='+_password+''; 
     tx.executeSql(sql,[],successLoginDB,errorCB); 
     } 

     function successLoginDB(tx,results){ 
     var len = results.rows.length; 
     var _name =$("[name='name']").val(); 
     if (len==1) {alert("Welcome "+_name);} 
     } 

回答

0

首先你的代碼很容易受到SQL Injection,使用ARGS修復:

function loginForm(){ 
    db.transaction(checkDB, errorCB); 
    $.mobile.changePage("#page5",{reverse:false,transition:"slide"}); 
    return false; 
} 

function checkDB(tx){ 
    var matric=$("[name='matric']").val(); 
    var password=$("[name='password']").val(); 
    var sql ='select * from STUDENT where matric = ? and password = ?'; 
    tx.executeSql(sql,[matric, password],successLoginDB,errorCB); 
} 

function successLoginDB(tx,results){ 
    var len = results.rows.length; 
    var name =$("[name='name']").val(); 
    if (len==1) {alert("Welcome "+name);} 
} 

關於錯誤;我確定你正在嘗試在cordova加載之前使用webSQL。您需要等待「DOM Ready」(用於讀取文本輸入)和「Deviceready」才能訪問WebSQL:

document.addEventListener("deviceready", onDeviceReady, false); 

var onDeviceReady = function() { 
    // Start here. 
} 

請發佈更多信息。

+0

這是我的開始代碼,但我仍然收到錯誤 – user2947488