<?php
if(isset($_POST["Add"]))
{
$name=$_POST["Emp_Username"];
$pass=$_POST["Emp_Password"];
$fname=$_POST["Emp_Fname"];
$lname=$_POST["Emp_Lname"];
$hph=$_POST["ContactNo_Home"];
$hp=$_POST["ContactNo_HP"];
$mail=$_POST["Emp_Email"];
$add=$_POST["Emp_Address"];
$age=$_POST["Emp_Age"];
$pos=$_POST["Position"];
$dept=$_POST["Dept_ID"];
mysql_query("
insert into employee(Dept_ID, Emp_Address, Emp_Age,
Position, >Emp_Username, Emp_Password, Emp_Fname, Emp_Lname,
ContactNo_Home, ContactNo_HP, Emp_Email)
>values('$dept','$add','$age','$pos','$name','$pass','$fname',
'$lname','$hph','$hp','$mail'>)
");
?>
<script type="text/javascript">
alert("Record saved.");
</script>
<?php } ?>
Asked
Active
Viewed 180 times
-1
Amal Murali
- 75,622
- 18
- 128
- 150
Jye Lim
- 3
- 2
-
2Use `UNIQUE` constraint on the username column in DB, in that case, insert with same username will fail (return an error). Then check for errors upon insert query. – poncha Aug 26 '13 at 12:43
-
Possible [Duplicate](http://stackoverflow.com/questions/8611044/check-if-username-already-exists-in-database-mysql-php) – Som Aug 26 '13 at 12:46
4 Answers
2
Create a unique key on username field, then detect error no 1062:
if (mysql_errno() == 1062) {
print 'username taken!';
}
Cups
- 6,901
- 3
- 26
- 30
1
A simple SELECT query should suffice:
SELECT COUNT(employee.Emp_Username) AS num
FROM employee
WHERE Emp_Username = 'USERNAME';
Wayne Whitty
- 19,513
- 7
- 44
- 66
-
-
I know. But I based my answer on the assumption that the database is correctly designed. OP asked how to detect if the username is already detected. He didn't ask how to prevent duplicate usernames. – Wayne Whitty Aug 26 '13 at 14:22
0
I hope this help you,
$query=mysql_query("select * from employee where Emp_Username=$name");
$row=mysql_num_rows($query);
if($row>0){
echo 'user exist';
}else{
echo 'User not exist';
}
Prabhakaran
- 3,900
- 15
- 46
- 113
0
Maybe you should validate field "user" with ajax before submit the form
$name = $_POST["Emp_Username"];
$results = mysqli_query($connecDB,"SELECT * FROM employee WHERE Emp_Username='$name'");
$name_exist = mysqli_num_rows($results);
if($username_exist) {
echo 'busy';
}else{
echo 'free';
}
santyas
- 96
- 1
- 2
- 12
-
1
-
1Two connections ask for a given username simultaneous , both get the answer 'name free' - both will register the name in the assumption it unique - without a unique constraint both will succeed. – Tom Regner Aug 26 '13 at 14:24