2016-04-10 21 views
0

PDO’da yazılmış bir kayıt komut dosyasına sahibim. Kaydolmadan sonra kullanıcının yeniden yönlendirilmesi gerekiyor, ancak bunun yerine aynı sayfada kalıyorlar.Kullanıcı kayıt olduktan sonra yeniden yönlendirilmiyor

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

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

İşte benim kayıt komut dosyası bulunuyor:

İşte benim veritabanı bağlantısı var

<?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> 

Ayrıca, ben sadece PDO MySQLi gelen kodumu açık - Ben gibi herhangi bariz hatalar var onunla tamamen deneyimli değil.

+0

sayfadaki herhangi hata buldu vardı? ya da error_reporting'i (E_ALL) ayarladınız; –

+0

daha ayrıntılı bilgi için burayı inceleyin http://stackoverflow.com/questions/12817846/header-is-not-redirecting-php –

+0

Neden mysqli ve PDO kullanıyorsunuz? -> 'if (mysqli_num_rows ($ checkusername) == 1)' çalışmaz. Daha fazla bilgi için [this] (http://stackoverflow.com/a/13195967/4982088) gönderin – Xorifelse

cevap

0

aşağıda gibi seçme deyimi kullanın -

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>