javascript - 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");
});
});
}
解决方案
你可以这样做,
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 查询中连接查询参数。
推荐阅读
- apache-spark - 具有 0 条记录的外部 Hive 表
- gitlab - GitLab openid-connect 与 Keycloak
- python - 无法使用python中的参数更改函数内部的全局变量
- networking - Kubernetes 通过 VirtualBox:Pod 无法访问互联网服务
- python - 打印 discord.py 语法错误,但我无法理解问题
- python - python将\ x00连接为字符串
- python - 如何在这里正确使用列表?
- python - Sudoko 求解器回溯算法不起作用
- regex - Notepad++ 将行尾移到最前面
- github - 对 GitHub Desktop 使用不同的 SSH 密钥