I have a simple form like so:
<fieldset id="membershipform">
<ul class="clearfix">
<li id="li-status">
<span>I am a:</span>
<menu>
<li><label for="student"><input disabled="disabled" type="radio" name="r1" id="student" checked="checked" /> Graduate</label></li>
<li><label for="student2"><input disabled="disabled" type="radio" name="r1" id="student2" /> Undergraduate</label></li>
</menu>
</li>
<li id="li-firstname">
<label for="firstname">First Name</label> <input disabled="disabled" type="text" placeholder="First Name" id="firstname" title="First Name" />
</li>
<li id="li-lastname">
<label for="lastname">Last Name</label> <input disabled="disabled" type="text" placeholder="Last Name" id="lastname" title="Last Name" />
</li>
<li id="li-email">
<label for="email">Email address</label> <input disabled="disabled" type="text" placeholder="Email address" id="email" title="Email address" />
</li>
<li id="li-buttons">
<input type="submit" value="Send Application ►" title="Send Application" />
</li>
</ui>
</fieldset>
What I want this to do is post the contents of the form to a table in my database, but if the email address only exists in the DB I want it to throw an error saying 'email already in use' to prevent the same user from applying.
Thanks
EDIT: To clarify what my question is as some have commented below that they are unsure what I am asking. I'm looking for some examples of how I could check that an email address that is in table that is being posted to doesn't already exist and therefore would throw an error. Hope that makes sense. Thanks again.
You could do something like this:
function email_exists($email)
{
$sql = mysql_db_query("SELECT email FROM table WHERE email = '" . $email . "'");
return(mysql_num_rows($sql)>0);
}
if(!email_exists($_GET['email'))
{
// insert database code here
}
There are two easy options here, generally speaking. The first is to do a simple check against the database using a SELECT to see how many results are returned. If it's >= 1, the email exits. The other option is to just throw a request at the database since it can be easier to make a single request and read the tea leaves than it is to do multiple, albeit small selects to see if the email exists before trying to insert a new row.
In any case, what you're looking to do is create a unique index on the email column in your MySQL database. That should take care of it either way -- it will speed up your searches for the email when doing any SELECT
s, and will cause any duplicate INSERT
s to fail. Because any queries that try to insert duplicate email addresses will fail, you can monitor for these failures PHP-side and assume an email exists when proper data returns them.
Here's a link to the MySQL CREATE INDEX
command. It should be as simple as running a command like this: CREATE UNIQUE INDEX email_index ON email
;