首页 > 解决方案 > JavaScript 登录功能无法使用 WebSQL

问题描述

我正在尝试使用 WebSQL 和 JavaScript 执行登录功能。我写了一个接受一个参数(email)的函数。该函数应该在数据库中搜索现有电子邮件,然后它应该返回一个警报,说明该电子邮件是否存在。

我还想使用该功能来搜索电子邮件是否存在相关密码,如果存在,它应该让用户登录,如果没有,它应该返回一条消息。所有帮助将不胜感激。提前致谢。

function email_exists(email) {
  mydb.transaction(function(transaction) {
    transaction.executeSql("select id from user where email= " + email + " ", [], function(tx, result) {
        alert("email exists");
      },
      function(error) {
        alert("Email does not exist");
      });
  });
}

标签: javascriptcordovaweb-sql

解决方案


你可以这样做,

function email_exists(email) {
    db.transaction(function (tx) { 
        tx.executeSql(
            'SELECT * FROM user WHERE email=? LIMIT 1',
            [email], 
            function (tx, result) { 
                if(result.rows.length){
                    // user found
                    alert("User found - ", result.rows[0]);
                }
                else {
                    // email does not exists
                    alert("User not found");
                }
            },
            null
        ); 
    });
}

这里要注意的一件重要事情是,您可以使用替换方法 ( ?) 来避免 sql 注入,而不是在 SQL 查询中连接查询参数。

这里进一步阅读


推荐阅读