在我的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();
}
?>
答案 0 :(得分:3)
在index.php
内include
- post.php
文件会立即将您转发回index.php
。
您需要以post.php
条件转发,或者需要重新修改逻辑。