designersvsoft
designersvsoft

Reputation: 1859

checking field in my sql

I am just a beginner in php. I am trying to write code for student registration form submission. I have the email field and name field in my form and i have to check the new entrants email address and name with the existing ones. If there is no previous existence my form should submit otherwise it should ask for the another email address and name. I used the following code.

<?php

$sel = "SELECT `varEmail`, `intStudentId`  FROM `tbl_registration` WHERE `varEmail` = '".$emailaddress."' AND `intStudentId` <> '".$Id."' ";
$res = mysql_query($sel);
if(mysql_num_rows($res) >0)
{ ?>
<h3 style="color:#FF0000;">The Email Address already exists. Please mention different Email Address.</h3>
<?php
}

else
     {

 $insertstudent="INSERT INTO `tbl_registration` (
`varFname` ,
`varLname` ,
`varAddress` ,
`varGender` ,
`intDOB` ,
`varEmail`
)
VALUES (
'".$fname."',  '".$lname."',  '".$address."',  '".$gender."',  '".$DOB."',  '".$emailaddress."'
);";
$insert_res = mysql_query($insertstudent);
     }




?>

I have to check the email duplication and name duplication only. How to modify my code? Thanks in advance.

Upvotes: 1

Views: 62

Answers (1)

mustafa
mustafa

Reputation: 745

if you want to check if email is used before

 WHERE varEmail = '$emailaddress' 

will be enough. If you want to check a certain student with certain email

 WHERE varEmail = '$emailaddress' AND name='$studentName'

If you want to check student name only

 WHERE  name='$studentName'

will do it, but this is problematic because many students may have same name.

Upvotes: 1

Related Questions