-1
<?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 } ?>
4

4 回答 4

2

Create a unique key on username field, then detect error no 1062:

if (mysql_errno() == 1062) {
    print 'username taken!';
}
于 2013-08-26T12:44:39.867 回答
1

A simple SELECT query should suffice:

SELECT COUNT(employee.Emp_Username) AS num 
FROM employee 
WHERE Emp_Username = 'USERNAME';
于 2013-08-26T12:44:50.127 回答
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';
    }
于 2013-08-26T12:44:17.120 回答
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';
}
于 2013-08-26T12:59:32.253 回答