如果声明似乎被忽略了

时间:2014-05-08 21:09:06

标签: php if-statement pdo http-headers

我有一个if语句,一旦它成立,我将重定向到另一个页面。它似乎对我不起作用。

<?php 
    require 'includes/config.php';

    session_start(); 

    if ( !empty($_POST['username'] && $_POST['password']) ){
    $gebruikersnaam = $_POST['username'];
    $wachtwoord = $_POST['password'];
    }

    try{
    $conn = new PDO('mysql:host=localhost;dbname=project_sync', $config['DB_USERNAME'], $config['DB_PASSWORD']);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $conn->prepare('SELECT * FROM employee WHERE gebruikersnaam = :gebruikersnaam');
    // Bind and Execute
    $stmt->execute(array(
            'gebruikersnaam' => $gebruikersnaam
        ));
    // Fetch Result
    while($result = $stmt->fetch()){
      if ($gebruikersnaam == $result['gebruikersnaam'] && $wachtwoord == $result['wachtwoord']){
    header('Location: http://localhost/project_sync2/dashboard.php');
    exit(); 
    }else{
    header('Location: http://localhost/project_sync2/index.php?set=loginerror');
    exit();
        set=loginerror';

    }
    }
    }catch (PDOExeption $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
?>

有谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:0)

用这个替换你的条件:

if ( !empty($_POST['username']) && !empty($_POST['password']) ){
[...]

答案 1 :(得分:0)

应该是:

if ( !empty($_POST['username']) && !empty($_POST['password']) ){

来自documentation

  

在PHP 5.5之前, empty()仅支持变量;其他任何事都会导致解析错误。