2014-04-02 73 views
0

我有一個腳本,我需要檢查用戶名和密碼,並顯示使用sqlite事務msg。狀態信息會在正確的條目中顯示適當的信息,但錯誤信息的警報信息不會顯示。可這個問題被固定.The腳本如下:提前其他條件與腳本與sqlite

var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024); 
db.transaction(function (tx) { 
tx.executeSql("CREATE TABLE IF NOT EXISTS Contact1(id unique, username TEXT NOT NULL, pass TEXT NOT NULL)"); 
tx.executeSql("INSERT INTO Contact1(id, username, pass) VALUES (1, 'pp1', '321')"); 
// tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")'); 
}); 


$(document).ready(function() // Call function when page is ready for load.. 

{ 
db.transaction(function (tx) { 
tx.executeSql("INSERT INTO Contact1(id, username, pass) VALUES (3, 'pp1', '321')"); 
}); 

$("#sub").click(function(){ 
var uname = $("#username").val(); 
var password = $("#password").val(); 
db.transaction(function (tx) { 
tx.executeSql('SELECT * FROM Contact1 WHERE username=?', [uname], function (tx, results) { 
var len = results.rows.length, i; 
// msg = "<p>Found rows: " + len + "</p>"; 
//document.querySelector('#status').innerHTML += msg; 
for (i = 0; i < len; i++){ 
// msg = "<p><b>" + results.rows.item(i).pass + "</b></p>"; 
//document.querySelector('#status').innerHTML += msg; 
if(results.rows.item(i).username == uname){ 
// alert("hello"); 
     tx.executeSql('SELECT * FROM Contact1 WHERE pass=?', [password], function (tx, results) { 
      var len = results.rows.length, i; 
      msg = "<p>Found rows: " + len + "</p>"; 
      document.querySelector('#status').innerHTML += msg; 
      for (i = 0; i < len; i++){ 
      msg = "<p><b>" + results.rows.item(i).username + "</b>==><b>" + results.rows.item(i).pass + "</b></p>"; 
      document.querySelector('#status').innerHTML += msg; 
      } 

     }, null); 

     } 
else 
    {//wrong username/password case 
    msg="<p><b>Please enter the right Username and password"+"</b></p>"; 
    document.querySelector('#status').innerHTML += msg; 
    } 
} 
}, null); 

});});}); 

感謝:d

回答

1

Suganthi指出的問題,但無論如何,我決定重寫你的代碼來解決問題,使代碼更優秀:

var db; 

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

function onDeviceReady() { 
    db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024); 
    db.transaction(function (tx) { 
     tx.executeSql("CREATE TABLE IF NOT EXISTS Contact1(id unique, username TEXT NOT NULL, pass TEXT NOT NULL)"); 
     tx.executeSql("INSERT INTO Contact1(id, username, pass) VALUES (1, 'pp1', '321')"); 
     tx.executeSql("INSERT INTO Contact1(id, username, pass) VALUES (3, 'pp1', '321')"); 
    }); 
} 

$("#sub").click(function() { 
    var uname = $("#username").val(); 
    var password = $("#password").val(); 
    db.transaction(function (tx) { 
     tx.executeSql('SELECT * FROM Contact1 WHERE username = ? AND pass = ?', [uname, password], function (tx, results) { 
      var len = results.rows.length; 
      if (len > 0) { 
       $('#status').append("<p>Found rows: " + len + "</p>"); 
       for (i = 0; i < len; i++) { 
        msg = "<p><b>" + results.rows.item(i).username + "</b>==><b>" + results.rows.item(i).pass + "</b></p>"; 
        $('#status').append(msg); 
       } 
      } else { 
       msg = "<p><b>Please enter correct username and password</b></p>"; 
       $('#status').append(msg); 
      } 
     }, null); 
    }); 
}); 
+0

非常感謝,這工作,我只是不得不刪除成功味精,並在那裏放置'導航URL'!乾杯 – rawatdeepesh

1

根據你的代碼
如果用戶名是錯誤的,它返回的len = 0,所以它不會進入else條件。 試試這個

$("#sub").click(function(){ 
var uname = $("#username").val(); 
var password = $("#password").val(); 
db.transaction(function (tx) { 
tx.executeSql('SELECT * FROM Contact1 ', function (tx, results) { 
var len = results.rows.length, i; 
for (i = 0; i < len; i++){ 
if(results.rows.item(i).username == uname &&results.rows.item(i).pass == password){ 
// alert("hello"); 
      msg = "<p><b>" + results.rows.item(i).username + "</b>==><b>" + results.rows.item(i).pass +  "</b></p>"; 
     document.querySelector('#status').innerHTML += msg; 
     } 
else 
{ 
msg="<p><b>Please enter the right Username and password"+"</b></p>"; 
document.querySelector('#status').innerHTML += msg; 
} 
} 
}, null); 
+0

這不是好...首先,「其他「將被多次調用,因爲沒有被接受的行。其次,選擇整桌不好。是的,它會「在這裏,現在」工作,但好的代碼呢? – Regent

+0

嗯,也許我以前的評論可以被看作是否定的,但我只是試圖說,最好不要選擇整個表格並將錯誤塊移出「for」循環 – Regent

+0

@Regent thanku我沒有否定它,感謝您的指導。 – suganthi