Here's the code.
<?php
//get each input from form into a variable
$firstname=$_POST['first_name'];
$lastname=$_POST['last_name'];
$gender=$_POST['gender'];
$district=$_POST['district'];
$email=$_POST['email'];
$password=$_POST['password'];
mysql_connect("localhost","root","");
mysql_select_db("maindb");
$result=mysql_query("SELECT email from user_info where email='$email' ");
//checking customer already exists
if(mysql_num_rows($result)>0)
{
echo "Email Already exists. Please enter a different email";
}
//inserting customer details
else
{
mysql_query("INSERT into user_info VALUES('$firstname','$lastname', NULL, '$gender','$district','$email','$password')");
echo "New User Added Successfully!";
}
?>
<form name="user" method="post" action="" onSubmit="return matchpasswords()">
<caption><h2>Registration Form</h2></caption>
<fieldset>
<label>FirstName</label><br/>
<input type="text" name="first_name" class="form-text" required>
</fieldset>
<fieldset>
<label>LastName</label><br/>
<input type="text" name="last_name" class="form-text" required>
</fieldset>
<fieldset class="radio">
<label>Gender</label><br/>
<input type="radio" name="gender" value="M"/>Male
<input type="radio" name="gender" value="F"/>Female
</fieldset>
<fieldset>
<label>District</label><br/>
<select name="district"required>
<option selected>Colombo</option>
<option>Kandy</option>
<option>Matara</option>
<option>Galle</option>
</select>
</fieldset>
<fieldset>
<label>Email</label><br/>
<input type="email" name="email" class="form-text" required>
</fieldset>
<fieldset>
<label>Password</label><br/>
<input type="password" name="password" class="form-text" required>
</fieldset>
<fieldset>
<label>ConfirmPassword</label><br/>
<input type="password" name="confirmation" class="form-text" required>
</fieldset>
<fieldset>
<input type="submit" value="Submit"/>
</fieldset>
<fieldset>
Already Signed Up? <a href="login.php"><h3>Login here.</h3></a>
</fieldset>
</form>
I was suggested to add this part to the beginning
if(isset($_POST["submit"]))
but after addition of this, you cannot insert anything into the database. Without the isset part you can insert into the datatable but with each refresh of the page and on page load a blank entry is added to the datatable and I get several errors.
Notice: Undefined index: first_name in D:\xampp\htdocs\embrace\useregistration.php on line 85
for each field in the sql table.