0

i am trying to put email validation if the user is already registered but the values are always inserted in my database even when the email already exists in database.

router.get('/save',function(req,res){
  var uname = req.query.username;
  var em = req.query.email;
  var passw = req.query.pass;
  var gender = req.query.gender;
  var dob = req.query.date;

  var con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"",
    database:"sherrydb"
  });
  con.query('select * from loginn where email= "'+req.query.email+'" ',function(err,row){
    if(err){
      con.query('insert into loginn(name, password, email, gender, date) VALUES("' + uname + '","' + passw + '","' + em + '","' + gender + '","' + dob + '")',
      function(err,rows,fields){   
        console.log(rows);
        res.send("inserted");
    });
  }
  else{
    alert("user already registered");
    return;
  }
  });

  con.end();
});
t.niese
  • 39,256
  • 9
  • 74
  • 101
Sherry Jain
  • 71
  • 1
  • 8
  • 1
    race conditions are still possible with this approach/method.. You should be adding a unique key in the table on the email column – Raymond Nijland Jul 13 '19 at 15:08
  • https://stackoverflow.com/a/24676032/7316335 you don't check for for a non existent row. You check if your err is for if there was an error in executing the query. Not if the row array is empty – Loveen Dyall Jul 13 '19 at 15:11

2 Answers2

1

You're only inserting when there is an error and your database query won't throw any error for both the cases, if it will not find any results or if it will find any results.

What will be in the results of those database queries -

  • When database has entry for that email will return array of rows containing that email.
  • when database doesn't have entry with that email it will return an empty array.

So, What you can do is, check for the query result length, if result length is 0 then you can insert and if greater than 0. You should send response of "Already Registered" or something.

0
router.get('/save',function(req,res){
  var uname = req.query.username;
  var em = req.query.email;
  var passw = req.query.pass;
  var gender = req.query.gender;
  var dob = req.query.date;

  var con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"",
    database:"sherrydb"
  });

  con.query('select * from loginn where email="'+em+'" ', (err, result) => {
    if (err) throw err;
    if (result.length>0){
      res.send("user already registered");
    }
    else{
      var conn = mysql.createConnection({
        host:"localhost",
        user:"root",
        password:"",
        database:"sherrydb"
      });
      conn.query('insert into loginn(name, password, email, gender, date) VALUES("' + uname + '","' + passw + '","' + em + '","' + gender + '","' + dob + '")',function(err,rows,fields){
        console.log(rows);
        res.send("inserted");
    });
    conn.end();

    }
  });
  con.end();
  });
Sherry Jain
  • 71
  • 1
  • 8