2016-09-30 110 views
-1

我在我的博客頁面上有一個基本的登錄表單,當我輸入正確的用戶名和密碼時,它說用戶名+密碼正確,但我沒有重定向到index1.php頁面。PHP提交表單無法正常工作

這裏是我的代碼:下面

<?php 
    require_once('header.php'); 
    require_once('config.php'); 

$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 

    //check to see if form has been submitted 
    if (isset($_POST['submit'])) { 
    if ($_POST['username'] == 'bruce' && ($_POST['password'] == 'wayne')) { 
     echo 'Correct username and password.'; 
    } 
    else { 
     echo 'Incorrect username or password. Please try again.'; 
    } 

    $result = mysqli_query($db, $sql); 
    $numrows = mysqli_num_rows($result); 

    if($numrows == 1) { 
     $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
     $_SESSION['USERNAME'] = $row['username']; 
     $_SESSION['USERID'] = $row['id']; 

     header("Location:"."index1.php"); 
    } 
    else { 
     header("Location"."login.php?error=1"); 
    } 
} 

    else { 
     if(isset($_GET['error'])) { 
      echo "Incorrect login, please try again!"; 
     } 
    } 
?> 

<form action="<?php echo $_SERVER['index1.php'] ?>" method="post"> 
<table id='login'> 
<tr> 
    <td>Username</td> 
    <td><input type="text" name="username"></td> 
</tr> 
<tr> 
    <td>Password</td> 
    <td><input type="password" name="password"></td> 
</tr> 
<tr> 
    <td></td> 
    <td><input type="submit" name="submit" value="Login!"></td> 
</tr> 
</table> 
</form> 

index1.php!

<?php 
    require("header.php"); 

    $db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 

    $sql = "SELECT entries.*, categories.cat FROM entries, categories 
     WHERE entries.cat_id = categories.id 
     ORDER BY dateposted DESC 
     LIMIT 1;"; 

    $result = mysqli_query($db,$sql); 
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 

    echo "<h2><a href= 'viewentry.php?id=" . $row['id'] . "'>" . $row['subject']. "</a></h2><br />"; 
    echo "<i>In <a href='viewcat.php?id=" . $row['cat_id'] . "'>" . $row['cat'] ."</a> - Posted on " . 
      date("D jS F Y g.iA", strtotime($row['dateposted'])) ."</i>"; 
    echo "<p>"; 
    echo nl2br($row['body']); 
    echo "</p>"; 

    require("footer.php"); 

?> 
+0

哪裏定義'$ db'? – Ghost

+1

您的'$ sql'語句在哪裏我看不到它。 – Franco

+0

您是否收到錯誤信息,如「已發送郵件頭」? –

回答

0

首先你需要在PHP中使用會話,如果再$ _SESSION因此未前使用session_start();變得正常陣列,而不再會被記住。

+0

可以顯示index1.php的代碼 –

+0

讓我知道...是否適用於你 –

0

除非使用輸出緩衝,否則在重定向之前無法回顯。我評論了你的回聲。

我一直都在使用它。您必須在重定向後致電退出。

/** 
* This is designed to redirect using a quick, easy function. 
*/ 
function gfRedir($_NewPath){ 
    header('Location: '.$_NewPath); 
    exit(); 
} 

你會用它喜歡:

<?php 
    require_once('header.php'); 
    require_once('config.php'); 

$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 

    //check to see if form has been submitted 
    if (isset($_POST['submit'])) { 
    if ($_POST['username'] == 'bruce' && ($_POST['password'] == 'wayne')) { 
     //you cannot echo before a redirect unless you are using output buffering. 
     //echo 'Correct username and password.'; 
    } 
    else { 
     //you cannot echo before a redirect unless you are using output buffering. 
     //echo 'Incorrect username or password. Please try again.'; 
    } 

    //where is this $sql at that you are passing into the below function? 

    $result = mysqli_query($db, $sql); 
    $numrows = mysqli_num_rows($result); 

    if($numrows == 1) { 
     $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
     $_SESSION['USERNAME'] = $row['username']; 
     $_SESSION['USERID'] = $row['id']; 

     gfRedir('index2.php'); 
    } 
    else { 

     gfRedir('login.php?error=1'); 
    } 
    } 

    else { 
     if(isset($_GET['error'])) { 
      echo "Incorrect login, please try again!"; 
     } 
    } 


    function gfRedir($_NewPath){ 
     header('Location: '.$_NewPath); 
     exit(); 
    } 
?> 

<form action="<?php echo $_SERVER['index1.php'] ?>" method="post"> 
<table id='login'> 
<tr> 
    <td>Username</td> 
    <td><input type="text" name="username"></td> 
</tr> 
<tr> 
    <td>Password</td> 
    <td><input type="password" name="password"></td> 
</tr> 
<tr> 
    <td></td> 
    <td><input type="submit" name="submit" value="Login!"></td> 
</tr> 
</table> 
</form> 
+0

試過這個,現在說的不正確的密碼或用戶名 – chris

+0

不,這只是一個片段,你需要粘貼這裏面你當前的代碼。 –

+0

讓我知道,如果您收到錯誤說「頭已發送」 –

-1

請嘗試使用此語法重定向。

header("Location:index1.php"); 
exit(); 

謝謝