Check if the mailing address exists, if it shows a message, if not, add to the table

I was hoping to get a little idea about this.

I have a form collecting first name, last name, city, state and email address. This form uses the jquery validation plugin and form plugin.

I would like to check if email exists ... if that is the case, then spit out a message that tells them that they already exist.

This is what I have for my update.php script, in which the form used to add names to the mysql database:

<?php $con = mysql_connect("host","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("table", $con); $sql="INSERT INTO wallnames (ID, firstname, lastname, city, state, email) VALUES('NULL','$_POST[firstname]','$_POST[lastname]','$_POST[city]','$_POST[state]','$_POST[email]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "<p style=\width:350px; height:200px; vertical-align:middle;\><strong>Thank you for adding your info</strong></p>"; mysql_close($con); ?> 
+4
source share
1 answer

You must create a unique index in the email column, after which the insertion will fail if you try to insert the same address twice.

 CREATE UNIQUE INDEX ux_wallnames_email ON wallnames (email) 

You can also run this query to check if mail exists:

 SELECT EXISTS (SELECT NULL FROM wallnames WHERE email = ' test@example.com ') 

It will return either 0 if the letter is not used, or 1 if it already exists in the table.

+4
source

Source: https://habr.com/ru/post/1311775/


All Articles