I was hoping to get a little insight on this.

What I have is a form collecting firstname, lastname, city, state, and email address. This form is using jquery validation plugin and the form plugin.

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

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

$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)

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



Are you sure that inserting 'NULL' as a string is what you want to do? Shouldn't that be NULL?

Written by Mark Byers

To be completely honest I am a little fresh with this stuff and saw an example and just plugged it in. It seems to be working with it. If I remove NULL... I get an error Mark - I am also not very sure I completely understand how to add a unique index

Written by Matthew

It should just be NULL without the quotation if you want the value NULL unless you want the word 'null'. If you are using Mysql, there should be a button you click to make a field a unique index. If not, just use what Mark wrote.

Written by ggfan

@Matthew: You should also note that what you've done is vulnerable to SQL injection attacks. Sorry if this sounds like too much work, but I suggest you ditch whatever tutorial/example site you're following and learn instead from a reliable source that teaches good practices. You'll save yourself a lot of time in the long run. See this thread for some ideas of where to start: stackoverflow.com/questions/2119083/…

Written by Mark Byers

@Mark... thanks for the insight. I will take a look and make sure that this gets done the right way.

Written by Matthew

@Mark... Okay have placed a unique index on the email column in my database. That has helped out a lot... I have an error message displaying if email is a dup.

Written by Matthew

Accepted Answer

You should create a unique index on the email column, then the insert will fail if you try to insert the same email twice.

CREATE UNIQUE INDEX ux_wallnames_email ON wallnames (email)

You can also run this query to test if an email already exists:

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

It will return either 0 if the email is unused, or 1 if it already exists in the table.

Written by Mark Byers
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki