Skip to content
Advertisement

Check for duplicate emails before querying

I’ve searched and look at other examples but as i’m new i’m having trouble translating over to my code. What I want to do is have it check the database to see if that email has already been entered. If it has I want it to tell them it has and that there is only one entry per person. Any help would be greatly appreciated. Keep in mind this is the first thing I’ve ever coded that connects to a database.

JavaScript

Advertisement

Answer

First things first: Switch over to Prepared Statements.

They are much safer and a more advanced way to access your database.

JavaScript

What I did is execute a separate query first searching for any entries already existing in the table with the user’s email address. So long as nothing’s found, the script continues.

Hopefully this has also given you an insight into how to execute prepared statements. These make sure that your database can’t be tampered with using injections, which is a lot off your back and ensures you can focus on coding efficient scripts rather than sanitizing user inputs.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement