Php 如果用户名已被使用或存在,如何添加将显示错误的条件

Php 如果用户名已被使用或存在,如何添加将显示错误的条件,php,mysql,if-statement,registration,Php,Mysql,If Statement,Registration,如果用户名已被使用或存在,如何添加将显示错误的条件 这是我的代码: <?php include '../connect.php'; ?> <?php if(isset($_POST['register'])){ $username = $_POST['username']; $password = $_POST['password']; $password2 = $_POST['password2'];

如果用户名已被使用或存在,如何添加将显示错误的条件

这是我的代码:

    <?php
    include '../connect.php';
    ?>

    <?php
    if(isset($_POST['register'])){

    $username = $_POST['username'];
    $password = $_POST['password'];
    $password2 = $_POST['password2'];
    $email = $_POST['email'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $bday = $_POST['bday'];
    $contact = $_POST['contact'];
    $address = $_POST['address'];
    $licenseno = $_POST['licenseno'];
    $gender = $_POST['gender'];
    $picture = $_POST['picture'];
    $avail = $_POST['availability'];

    if(empty($username)||empty($email)||empty($password)||empty($password2)||empty($fname)||empty($lname)||empty($bday)||empty($contact)||empty($address)||empty($licenseno)||empty($gender)){
        $msg="*PLEASE FILL IN REQUIRED FIELDS*";
        header('Location:RegisterT.php?msg='.$msg.'');
    }
    else{

         if($password != $password2){
             $msg="Password Mismatch!";
             header('Location:registerT.php?msg='.$msg.'');
             }
         else{

             mysql_query("INSERT INTO therapist (`id`, `username`, `email`, `password`, `fname`, `lname`, `bday`, `contact`, `address`, `licenseno`, `gender`, `picture`, `about`, `availability` )VALUES
             ('', '$username','$email','$password','$fname','$lname','$bday','$contact','$address','$licenseno','$gender','default.jpg','Currently no information to show','Available')");
             $msg="Registration Successful";
             header('Location:LoginT.php?msg='.$msg.'');

        }
    }


}

?>


您在insert中也有一个错误。mysqli_查询将连接作为第一个参数,将查询作为第二个参数。您只有一个参数。
if($password != $password2) {
         $msg="Password Mismatch!";
         header('Location:registerT.php?msg='.$msg.'');
}
else {
    //Check for username in database. If exists, show error.
    $sql = "SELECT username FROM therapist WHERE username = " . $username;
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        $msg="Username already exist";
        header('Location:LoginT.php?msg='.$msg.'');
    }

     mysql_query("INSERT INTO therapist (`id`, `username`, `email`, `password`, `fname`, `lname`, `bday`, `contact`, `address`, `licenseno`, `gender`, `picture`, `about`, `availability` )VALUES
     ('', '$username','$email','$password','$fname','$lname','$bday','$contact','$address','$licenseno','$gender','default.jpg','Currently no information to show','Available')");
     $msg="Registration Successful";
     header('Location:LoginT.php?msg='.$msg.'');

}