HTML 带有 PHP 的表单:提交时未设置 $_POST
HTML Form with PHP: $_POST is not setting on submit
背景:
我已经查看了这个问题和解决方案的变体,但没有找到适用于我的情况的变体。我知道我很可能忽略了一些非常明显的事情,但已经过去了几个小时,我无法确定它是什么。我正在学习本教程,但将它变成我自己的:https://www.taniarascia.com/create-a-simple-database-app-connecting-to-mysql-with-php/.
发生了什么: 当我点击提交按钮时,没有任何反应。我在页面加载时和点击提交后看到屏幕上显示“未设置”,所以我知道我在我的 else 块中,这意味着 $_POST 没有被设置。
我希望发生的事情: 我希望在页面加载时看到“未设置”,但是一旦我填写了表单并单击提交,我希望看到我的 echo 语句中的“set”表明我在我的 if 语句中并且 isset($_POST['submit']) 是 true/successful,后面是“Success”表明 try 代码块成功,加上我在数据库中出现的表单字段中输入的值。不过现在,如果我能展示第一个“设置”,我会很高兴。
我试过:我试过将 php 分解成一个单独的文件并通过 action="[=34= 将其链接到表单]”,但我得到了相同的结果。我也尝试过使用“get”而不是“post”作为方法,使用 $_REQUEST 而不是 $_POST,但同样的结果 - 显示“未设置”,数据库中没有数据。
这是我的代码:
<?php
if (isset($_POST['submit'])) {
require "../config.php";
echo "set";
try {
$connection = new PDO($dsn, $username, $password, $options);
$new_item = array(
"luggage" => $_POST['luggage'],
"category" => $_POST['category'],
"item" => $_POST['item'],
"description" => $_POST['description']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"stuff",
implode(", ", array_keys($new_item)),
":" . implode(", :", array_keys($new_item))
);
$statement = $connection->prepare($sql);
$statement->execute($new_item);
echo "Success";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else {
echo "not set";
}
?>
<?php include "header.php"; ?>
<h1>Add An Item</h1>
<form method="post">
<label for="item">Luggage</label>
<input type="text" name="luggage" id="luggage"></form>
<label for="item">Category</label>
<input type="text" name="category" id="category"></form>
<label for="item">Item</label>
<input type="text" name="item" id="item"></form>
<label for="description">Description</label>
<input type="text" name="description" id="description">
<input type="submit" name="submit" value="submit">
</form>
<a href="index.php">Back to home</a>
<?php include "footer.php"; ?>
您在表单 </form>
最后结束前多次关闭表单 </form>
,因此提交不在表单中。删除这些:
<label for="item">Luggage</label>
<input type="text" name="luggage" id="luggage"></form> ***HERE***
<label for="item">Category</label>
<input type="text" name="category" id="category"></form> ***HERE***
<label for="item">Item</label>
<input type="text" name="item" id="item"></form> ***HERE***
背景: 我已经查看了这个问题和解决方案的变体,但没有找到适用于我的情况的变体。我知道我很可能忽略了一些非常明显的事情,但已经过去了几个小时,我无法确定它是什么。我正在学习本教程,但将它变成我自己的:https://www.taniarascia.com/create-a-simple-database-app-connecting-to-mysql-with-php/.
发生了什么: 当我点击提交按钮时,没有任何反应。我在页面加载时和点击提交后看到屏幕上显示“未设置”,所以我知道我在我的 else 块中,这意味着 $_POST 没有被设置。
我希望发生的事情: 我希望在页面加载时看到“未设置”,但是一旦我填写了表单并单击提交,我希望看到我的 echo 语句中的“set”表明我在我的 if 语句中并且 isset($_POST['submit']) 是 true/successful,后面是“Success”表明 try 代码块成功,加上我在数据库中出现的表单字段中输入的值。不过现在,如果我能展示第一个“设置”,我会很高兴。
我试过:我试过将 php 分解成一个单独的文件并通过 action="[=34= 将其链接到表单]”,但我得到了相同的结果。我也尝试过使用“get”而不是“post”作为方法,使用 $_REQUEST 而不是 $_POST,但同样的结果 - 显示“未设置”,数据库中没有数据。
这是我的代码:
<?php
if (isset($_POST['submit'])) {
require "../config.php";
echo "set";
try {
$connection = new PDO($dsn, $username, $password, $options);
$new_item = array(
"luggage" => $_POST['luggage'],
"category" => $_POST['category'],
"item" => $_POST['item'],
"description" => $_POST['description']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"stuff",
implode(", ", array_keys($new_item)),
":" . implode(", :", array_keys($new_item))
);
$statement = $connection->prepare($sql);
$statement->execute($new_item);
echo "Success";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else {
echo "not set";
}
?>
<?php include "header.php"; ?>
<h1>Add An Item</h1>
<form method="post">
<label for="item">Luggage</label>
<input type="text" name="luggage" id="luggage"></form>
<label for="item">Category</label>
<input type="text" name="category" id="category"></form>
<label for="item">Item</label>
<input type="text" name="item" id="item"></form>
<label for="description">Description</label>
<input type="text" name="description" id="description">
<input type="submit" name="submit" value="submit">
</form>
<a href="index.php">Back to home</a>
<?php include "footer.php"; ?>
您在表单 </form>
最后结束前多次关闭表单 </form>
,因此提交不在表单中。删除这些:
<label for="item">Luggage</label>
<input type="text" name="luggage" id="luggage"></form> ***HERE***
<label for="item">Category</label>
<input type="text" name="category" id="category"></form> ***HERE***
<label for="item">Item</label>
<input type="text" name="item" id="item"></form> ***HERE***