这个网页有一个重定向循环-php


This webpage has a redirect loop - php

在我的index.php页面上得到了一个简单的表单,提交后在我的post.php页面上进行处理。

index.php

    <form id="form" action="post.php" method="post">
    Name:<br>
    <input type="text" name="name"/> <br>
    Gender:<br>
    <input type="text" name="gender"/> <br>
    Age:<br>
    <input type="number" name="age" min="1" max="99"/> <br>
    <input id="submit" type="submit" value="Input">
    </form>
<div class="data">
    <?php
        include ('post.php');
        foreach ($result as $row) {
        echo $row['name']  . " ";
        echo $row['gender'] . " " ;
        echo $row['age']  . "<br>" . " ";
        }
    ?> 
</div>

post.php下面是我的post.php页面的代码,顶部是我的标题,但当我提交表单时,我一直收到同样的错误。

<?php
header("Location: index.php");
try {
    $dbh = new PDO('sqlite:mydb.sqlite3');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->exec("CREATE TABLE IF NOT EXISTS test (
             name VARCHAR(30),
             gender VARCHAR(30),
             age INTEGER)"
             );
if (!empty($_POST)) {
  $stmt = $dbh->prepare("INSERT INTO test (name, gender, age) VALUES (:name, :gender, :age)");
  $stmt->execute(array(':name' => $_POST['name'], ':gender' => $_POST['gender'],':age' => $_POST['age']));
  $title = $_POST['name'];
  $message = $_POST['gender'];
  $age = $_POST['age'];
}
    $result = $dbh->query('SELECT * FROM test');
    $dbh = null;
}
    catch(PDOException $e) {
    echo $e->getMessage();
}       
?> 

index.php中,您正在include处理post.php文件,该文件会立即将您转发回index.php

您需要使post.php中的转发成为有条件的,或者您需要修改您的逻辑。