2016-04-10 7 views
0

Ich habe ein Registrierungsskript in PDO geschrieben. Der Nutzer soll nach der Anmeldung neu geleitet werden, bleibt aber auf der gleichen Seite.Benutzer wird nicht nach der Registrierung neu geleitet

Hier ist meine Datenbankverbindung:

$db_username = "username"; 
$db_password = "password"; 

$con = new PDO("mysql:host=localhost;dbname=database", $db_username, $db_password); 

Hier ist meine Registrierung Skript:

<?php 

if(isset($_POST['submit'])) { 

$username = $_POST['username']; 
$password = $_POST['password']; 
$email = $_POST['email']; 

if (empty($username)) { 
    $errorusername = 'Please enter a username'; 
}else{ 

    if (empty($password)) { 
     $errorpassword = 'Please enter a password'; 
    }else{ 

     if (empty($email)) { 
      $erroremail = 'Please enter an email.'; 
     }else{ 

      $password = md5($password); 

      $checkusername = $stmt = $con->prepare("SELECT * FROM users WHERE username=':username'"); 
      if (mysqli_num_rows($checkusername) == 1) 
      { 
       echo "Username already exists."; 
      }else{ 

       $status = 'Hello there!'; 
       $about = 'Hello!'; 

       $stmt = $con->prepare("INSERT INTO users (username, password, email, status, about) VALUES (:username, :password, :email, :status, :about)"); 

       $stmt->bindParam(':username', $_POST['username']); 
       $stmt->bindParam(':password', md5($_POST['password'])); 
       $stmt->bindParam(':email', $_POST['email']); 
       $stmt->bindParam(':status', $status); 
       $stmt->bindParam(':about', $about); 
       $stmt->execute(); 

       header('Location: index.php'); 
      } 
     } 
    } 
} 
} 

?> 

<form method="post"> 
<input type="text" name="username"> 
<input type="password" name="password"> 
<input type="email" name="email"> 
<?php echo $errorusername = !empty($errorusername) ? $errorusername : ''; ?> 
<?php echo $errorpassword = !empty($errorpassword) ? $errorpassword : ''; ?> 
<?php echo $erroremail = !empty($erroremail) ? $erroremail : ''; ?> 
<input type="submit" name="submit"> 
</form> 

Auch ich nur dann eingeschaltet nur meinen Code von MYSQLI zu PDO - gibt es irgendwelche offensichtlichen Fehler, wie ich bin nicht vollständig damit erfahren.

+0

haben Sie auf Seite einen Fehler gefunden? oder du hast error_reporting (E_ALL) gesetzt; –

+0

Überprüfen Sie diese für weitere Details http://StackOverflow.com/Questions/12817846/header-is-Not-Redirecting-PHP –

+0

Warum verwenden Sie mysqli und PDO? -> 'if (mysqli_num_rows ($ checkusername) == 1)' wird nicht funktionieren. Siehe [dies] (http://Stackoverflow.com/a/13195967/4982088) Post – Xorifelse

Antwort

0

verwenden Ihre select-Anweisung wie unten -

if(isset($_POST['submit'])) { 

$username = $_POST['username']; 
$password = $_POST['password']; 
$email = $_POST['email']; 

if(empty($username)) { 
$errorusername = 'Please enter a username'; 
}else{ 

if(empty($password)) { 
$errorpassword = 'Please enter a password'; 
}else{ 

if(empty($email)) { 
$erroremail = 'Please enter an email.'; 
}else{ 

$password = md5($password); 

$stmt = $con->prepare("SELECT * FROM users WHERE username=':username'"); 
$stmt->bindParam(':username', $username); 
$stmt->execute(); 
$total = $stmt->rowCount(); 
if($total == 1) 
{ 
echo "Username already exists."; 
}else{ 

$status = 'Hello there!'; 
$about = 'Hello!'; 


$stmt = $con->prepare("INSERT INTO users (username, password, email, status, about) VALUES (:username, :password, :email, :status, :about)"); 

$stmt->bindParam(':username', $_POST['username']); 
$stmt->bindParam(':password', md5($_POST['password'])); 
$stmt->bindParam(':email', $_POST['email']); 
$stmt->bindParam(':status', $status); 
$stmt->bindParam(':about', $about); 
$stmt->execute(); 

header('Location: index.php'); 
} 
} 
} 
} 
} 

?> 

<form method="post"> 
<input type="text" name="username"> 
<input type="password" name="password"> 
<input type="email" name="email"> 
<?php echo $errorusername = !empty($errorusername) ? $errorusername : ''; ?> 
<?php echo $errorpassword = !empty($errorpassword) ? $errorpassword : ''; ?> 
<?php echo $erroremail = !empty($erroremail) ? $erroremail : ''; ?> 
<input type="submit" name="submit"> 
</form> 
Verwandte Themen