简体   繁体   中英

JavaScript login function not working using WebSQL

I am attempting to do a login function using WebSQL and JavaScript. I have written a function the takes one argument( email ). The function is supposed to search the database for an existing email then it should return an alert stating whether the email exists or not.

I further wan to use that function to search if aa correlating password exits for the email, if it does, it should log the user in, if not it should return a message. All help would greatly appreciated. Thanks in advance.

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");
      });
  });
}

You can do it like this,

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
        ); 
    });
}

One Important thing to note here is that You can avoid sql injection using the substitution approach ( ? ) rather than concatenating query params in SQL query.

Read further from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM