2016-09-29 1 views
-1

Wie kann ich eine Bedingung hinzufügen, die einen Fehler anzeigt, wenn der Benutzername bereits vergeben ist oder existiert?Wie kann ich eine Bedingung hinzufügen, die einen Fehler anzeigt, wenn der Benutzername bereits vergeben ist oder existiert?

Dies ist mein Code:

<?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.''); 

     } 
    } 


} 

?> 

Antwort

0
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.''); 

} 
+0

Sie haben auch einen Fehler im Einsatz. mysqli_query nimmt Verbindung als ersten Parameter und Abfrage als zweite. Sie haben nur einen Parameter. – Nabeel

Verwandte Themen