PHP 中 POST 方法的问题
Issues with POST method in PHP
问题:
未定义 POST 表单提交后的变量。
已完成研究和故障排除:
- 阅读这里的大量问题,几乎所有问题都与表单字段上没有姓名标签有关。我的所有字段都有标签和 ID。
- 将我的 PHP.ini 配置为将 $HTTP_RAW_POST_DATA 设置为 -1
- 已学习 PHP.net 和 W3SChools
中的教程
此时我迷路了。数据只是拒绝 post,它全部返回未定义。下面是显示问题的 HTML、PHP 和两个屏幕截图。
我在 Windows 上使用 PHPStorm 的内置服务器。
signup.html
<div class="text-center col-md-4 col-md-offset-4">
<form id="user_signup" class="form-horizontal signInFields" action="../php/register.php" method="POST">
<input type="text" id="first_name" name="first_name" placeholder="First Name">
<input type="text" id="last_name" name="last_name" placeholder="Last Name">
<input type="email" id="user_email" name="user_email" placeholder="Email">
<input type="text" id="user_id" name="user_id" placeholder="User ID">
<input type="password" id="user_password" name="user_password" placeholder="Password">
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
<button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
</form>
register.php
// Variables from the sign-up form POST action
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$user_email = $_POST["user_email"];
$user_id = $_POST["user_id"];
$user_password = $_POST["user_password"];
$confirm_password = $_POST["confirm_password"];
// Add a new user to the database
$conn = new mysqli($servername, $username, $password);
$testQuery = mysql_insert($first_name,$last_name,$user_email,$user_id,$user_password);
if($conn->query($testQuery) === TRUE){
echo "New Record Created Successfully!";
} else {
echo "Error: " . $testQuery . "<br>" . $conn->error;
}
$conn->close();
已填写字段的表单
提交后输出:
此时我很困惑。据我从 W3Schools、PHP.net 和这里的各种问题得知,我已经正确设置了它。然而,有些事情显然不对劲。任何帮助将不胜感激。
编辑:
在这方面您正在使用 PHPStorm 的 built-in web server, which has some issues right now especially with POST requests, e.g. WEB-17317. You can also refer to this 。
这可能是您无法处理表单的原因。
因此,最好的解决方案是将您的 PHPStorm 配置为使用您的本地 Web 服务器,XAMPP 在您的情况下,用于您当前的项目。按照以下步骤进行操作:
- 配置本地服务器的虚拟主机,将当前项目目录作为根目录
- 确保您在上述步骤配置后可以访问项目文件。例如:
http://localhost/signup.html
应该可以通过浏览器访问。
- 现在,在 PHPStorm 中,转到 运行 -> 编辑配置 和 select 每个文件并为每个文件配置 url如下图所示。例如。 signup.html 文件使用
http://localhost/signup.html
。
- 您不需要为项目中的所有文件设置 url(第 3 步)。只需为 default(起始)文件执行此操作 - 例如index.html 或 login.html 或 signup.html。表单中与
<a>
或 action="register.php"
链接的所有其他文件将自动由本地服务器提供。
现在,您可以使用 PHPStorm 中的 green 按钮 运行 您的 HTML/PHP 文件,它将使用您在步骤 1 中配置的 url 在您的浏览器中打开这些文件。您应该能够毫无问题地处理您的表单。
但如果您的问题是由其他原因引起的,请考虑检查以下几点:
- 表单字段应具有 name 属性,例如"name='first_name"` 在 signup.html 文件中。
echo $_SERVER["REQUEST_METHOD"] == "POST"
在 register.php 文件中写入时打印 1
,这意味着您的表单已成功提交。
- 您的 php.ini 中的值
post_max_size
设置为 #M 格式,例如10M
,任何其他格式如 10MB
都是无效的。参考 php.net.
- 检查
echo file_get_contents("php://input");
是否以以下格式显示数据:first_name=John&last_name=Doe ....
。参考 php.net.
- 检查
var_dump($_POST)
是否在数组中显示表单数据。
希望对您有所帮助。欢迎评论。
您可以使用输入类型按钮代替您正在使用的按钮,在这种情况下,您可以使用 ajax 发送数据。
试试这个
register.php
<?php
$errors = [];
$fields =
[
'first_name',
'last_name',
'user_email',
'user_id',
'user_password',
'confirm_password',
];
$data = [];
if( !empty( $_POST ) )
{
foreach ($fields as $field)
{
if( isset( $_POST[$field] ) )
{
$data[$field] = $_POST[$field];
}
else
{
//empty field handling
$errors[$field] = $field.' required!';
}
}
}
if( empty($errors) )
{
// Add a new user to the database
$conn = new mysqli($servername, $username, $password);
$testQuery = mysql_insert
(
$data['first_name'],
$data['last_name'],
$data['user_email'],
$data['user_id'],
$data['user_password']
);
if($conn->query($testQuery) === TRUE)
{
echo "New Record Created Successfully!";
}
else
{
echo "Error: " . $testQuery . "<br>" . $conn->error;
}
$conn->close();
}
signup.php
<?php require('path/to/register.php'); ?>
<form id="user_signup" class="form-horizontal signInFields" action="" method="post">
<input type="text" id="first_name" name="first_name" placeholder="First Name">
<input type="text" id="last_name" name="last_name" placeholder="Last Name">
<input type="email" id="user_email" name="user_email" placeholder="Email">
<input type="text" id="user_id" name="user_id" placeholder="User ID">
<input type="password" id="user_password" name="user_password" placeholder="Password">
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
<button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
</form>
旁注(如果您是新手)
使用method="post"代替method="POST"(只是标准)
您没有验证输入字段(危险)
我复制了您的表格和 register.php
文件的(部分),没有任何问题。数据 post 符合预期。我之前遇到过类似的问题,结果证明这是一个奇怪的重定向问题,所以我的 POST 数据在到达我的控制器时丢失了。遗憾的是,我只能提供一些调试建议。
我确实想提一下,一旦你弄清楚了你的 POST 问题,你将需要查看你的数据库交互的东西......你创建了一个 mysqli
对象,但调用了一个函数称为 mysql_insert
(一个我从未见过并且在 php.net 上找不到的函数,所以我倾向于认为它不会做任何事情)。快速搜索将带您找到许多关于如何使用 mysqli
的详尽教程和演练,所以我会推迟这些而不是试图在这里解释所有内容(因为您仍在尝试解决 POST 问题。
1] 您不必使用 PDO; mysqli 很好,只要你正确使用它,完全可以接受 (here's some information about both from php.net directly)。
2] 我从未见过指定在 <form>
标签中使用小写字母 "post" 而不是 "POST" 的标准,但我会喜欢 link !
3] <input type="submit" value="text">
和 <button type="submit">text</button>
在 功能上 相同,因为两者都将 post 形式没问题。两者之间存在一些差异(例如,能够动态设置 "value" 而不是 text 之类的 "innerHTML",但这是另一个问题。
至于进一步的调试工作...在 register.php
中,就像 Orions 提到的那样,我很好奇 $_SERVER['REQUEST_METHOD']
报告了什么。在该文件的开头,尝试 die($_SERVER['REQUEST_METHOD'])
并查看它的内容。如果它显示 "POST",那么您就走对了。如果您在该文件的开头执行 var_dump($_POST);
,然后执行 die/exit,您会看到什么?
我记得有 2 个例子是重定向抢走了我的 POST 变量,都是由 mod_rewrites 引起的。一个是将请求从 mydomain.com
重定向到 www.mydomain.com
,并且由于我表单的 action
明确地是 mydomain.com
,所以一切都丢失了。另一个问题是 mod_rewrite 从请求中追加或删除尾随 /
(我不记得是哪个),但这也有相同的基本结果。如果您有一个 .htaccess
文件,请检查以确保没有恶意或陷阱发生。
其他要检查的东西:
a] 在你的 php.ini
中,确保 variables_order
在某处有 "P" (reference)
b] 同样在你的 php.ini 中,确保 post_max_size
是合理的;我怀疑这是问题所在,因为你没有上传文件,但如果它被设置为非常小的东西,我可以想象这可能是一个问题
c] 如果您 post 从 http
页面转到 https
页面,有时会删除 POST 数据(这可能是服务器-具体的,因为我觉得我有时看过这部作品,而不是其他作品)
d] 你有没有按照其他地方的建议得到 file_get_contents("php://input");
?它是否包含您的数据(看起来像这样:key1=value1&key2=value2
)还是太空白了?
e] 如果您在另一个页面上创建另一个表单并将其 POST 保存到另一个文件中,$_POST 数组会被填充吗?
问题:
未定义 POST 表单提交后的变量。
已完成研究和故障排除:
- 阅读这里的大量问题,几乎所有问题都与表单字段上没有姓名标签有关。我的所有字段都有标签和 ID。
- 将我的 PHP.ini 配置为将 $HTTP_RAW_POST_DATA 设置为 -1
- 已学习 PHP.net 和 W3SChools 中的教程
此时我迷路了。数据只是拒绝 post,它全部返回未定义。下面是显示问题的 HTML、PHP 和两个屏幕截图。
我在 Windows 上使用 PHPStorm 的内置服务器。
signup.html
<div class="text-center col-md-4 col-md-offset-4">
<form id="user_signup" class="form-horizontal signInFields" action="../php/register.php" method="POST">
<input type="text" id="first_name" name="first_name" placeholder="First Name">
<input type="text" id="last_name" name="last_name" placeholder="Last Name">
<input type="email" id="user_email" name="user_email" placeholder="Email">
<input type="text" id="user_id" name="user_id" placeholder="User ID">
<input type="password" id="user_password" name="user_password" placeholder="Password">
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
<button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
</form>
register.php
// Variables from the sign-up form POST action
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$user_email = $_POST["user_email"];
$user_id = $_POST["user_id"];
$user_password = $_POST["user_password"];
$confirm_password = $_POST["confirm_password"];
// Add a new user to the database
$conn = new mysqli($servername, $username, $password);
$testQuery = mysql_insert($first_name,$last_name,$user_email,$user_id,$user_password);
if($conn->query($testQuery) === TRUE){
echo "New Record Created Successfully!";
} else {
echo "Error: " . $testQuery . "<br>" . $conn->error;
}
$conn->close();
已填写字段的表单
提交后输出:
此时我很困惑。据我从 W3Schools、PHP.net 和这里的各种问题得知,我已经正确设置了它。然而,有些事情显然不对劲。任何帮助将不胜感激。
编辑:
在这方面您正在使用 PHPStorm 的 built-in web server, which has some issues right now especially with POST requests, e.g. WEB-17317. You can also refer to this
这可能是您无法处理表单的原因。
因此,最好的解决方案是将您的 PHPStorm 配置为使用您的本地 Web 服务器,XAMPP 在您的情况下,用于您当前的项目。按照以下步骤进行操作:
- 配置本地服务器的虚拟主机,将当前项目目录作为根目录
- 确保您在上述步骤配置后可以访问项目文件。例如:
http://localhost/signup.html
应该可以通过浏览器访问。 - 现在,在 PHPStorm 中,转到 运行 -> 编辑配置 和 select 每个文件并为每个文件配置 url如下图所示。例如。 signup.html 文件使用
http://localhost/signup.html
。- 您不需要为项目中的所有文件设置 url(第 3 步)。只需为 default(起始)文件执行此操作 - 例如index.html 或 login.html 或 signup.html。表单中与
<a>
或action="register.php"
链接的所有其他文件将自动由本地服务器提供。
- 您不需要为项目中的所有文件设置 url(第 3 步)。只需为 default(起始)文件执行此操作 - 例如index.html 或 login.html 或 signup.html。表单中与
现在,您可以使用 PHPStorm 中的 green 按钮 运行 您的 HTML/PHP 文件,它将使用您在步骤 1 中配置的 url 在您的浏览器中打开这些文件。您应该能够毫无问题地处理您的表单。
但如果您的问题是由其他原因引起的,请考虑检查以下几点:
- 表单字段应具有 name 属性,例如"name='first_name"` 在 signup.html 文件中。
echo $_SERVER["REQUEST_METHOD"] == "POST"
在 register.php 文件中写入时打印1
,这意味着您的表单已成功提交。- 您的 php.ini 中的值
post_max_size
设置为 #M 格式,例如10M
,任何其他格式如10MB
都是无效的。参考 php.net. - 检查
echo file_get_contents("php://input");
是否以以下格式显示数据:first_name=John&last_name=Doe ....
。参考 php.net. - 检查
var_dump($_POST)
是否在数组中显示表单数据。
希望对您有所帮助。欢迎评论。
您可以使用输入类型按钮代替您正在使用的按钮,在这种情况下,您可以使用 ajax 发送数据。
试试这个
register.php
<?php
$errors = [];
$fields =
[
'first_name',
'last_name',
'user_email',
'user_id',
'user_password',
'confirm_password',
];
$data = [];
if( !empty( $_POST ) )
{
foreach ($fields as $field)
{
if( isset( $_POST[$field] ) )
{
$data[$field] = $_POST[$field];
}
else
{
//empty field handling
$errors[$field] = $field.' required!';
}
}
}
if( empty($errors) )
{
// Add a new user to the database
$conn = new mysqli($servername, $username, $password);
$testQuery = mysql_insert
(
$data['first_name'],
$data['last_name'],
$data['user_email'],
$data['user_id'],
$data['user_password']
);
if($conn->query($testQuery) === TRUE)
{
echo "New Record Created Successfully!";
}
else
{
echo "Error: " . $testQuery . "<br>" . $conn->error;
}
$conn->close();
}
signup.php
<?php require('path/to/register.php'); ?>
<form id="user_signup" class="form-horizontal signInFields" action="" method="post">
<input type="text" id="first_name" name="first_name" placeholder="First Name">
<input type="text" id="last_name" name="last_name" placeholder="Last Name">
<input type="email" id="user_email" name="user_email" placeholder="Email">
<input type="text" id="user_id" name="user_id" placeholder="User ID">
<input type="password" id="user_password" name="user_password" placeholder="Password">
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
<button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
</form>
旁注(如果您是新手)
使用method="post"代替method="POST"(只是标准)
您没有验证输入字段(危险)
我复制了您的表格和 register.php
文件的(部分),没有任何问题。数据 post 符合预期。我之前遇到过类似的问题,结果证明这是一个奇怪的重定向问题,所以我的 POST 数据在到达我的控制器时丢失了。遗憾的是,我只能提供一些调试建议。
我确实想提一下,一旦你弄清楚了你的 POST 问题,你将需要查看你的数据库交互的东西......你创建了一个 mysqli
对象,但调用了一个函数称为 mysql_insert
(一个我从未见过并且在 php.net 上找不到的函数,所以我倾向于认为它不会做任何事情)。快速搜索将带您找到许多关于如何使用 mysqli
的详尽教程和演练,所以我会推迟这些而不是试图在这里解释所有内容(因为您仍在尝试解决 POST 问题。
1] 您不必使用 PDO; mysqli 很好,只要你正确使用它,完全可以接受 (here's some information about both from php.net directly)。
2] 我从未见过指定在 <form>
标签中使用小写字母 "post" 而不是 "POST" 的标准,但我会喜欢 link !
3] <input type="submit" value="text">
和 <button type="submit">text</button>
在 功能上 相同,因为两者都将 post 形式没问题。两者之间存在一些差异(例如,能够动态设置 "value" 而不是 text 之类的 "innerHTML",但这是另一个问题。
至于进一步的调试工作...在 register.php
中,就像 Orions 提到的那样,我很好奇 $_SERVER['REQUEST_METHOD']
报告了什么。在该文件的开头,尝试 die($_SERVER['REQUEST_METHOD'])
并查看它的内容。如果它显示 "POST",那么您就走对了。如果您在该文件的开头执行 var_dump($_POST);
,然后执行 die/exit,您会看到什么?
我记得有 2 个例子是重定向抢走了我的 POST 变量,都是由 mod_rewrites 引起的。一个是将请求从 mydomain.com
重定向到 www.mydomain.com
,并且由于我表单的 action
明确地是 mydomain.com
,所以一切都丢失了。另一个问题是 mod_rewrite 从请求中追加或删除尾随 /
(我不记得是哪个),但这也有相同的基本结果。如果您有一个 .htaccess
文件,请检查以确保没有恶意或陷阱发生。
其他要检查的东西:
a] 在你的 php.ini
中,确保 variables_order
在某处有 "P" (reference)
b] 同样在你的 php.ini 中,确保 post_max_size
是合理的;我怀疑这是问题所在,因为你没有上传文件,但如果它被设置为非常小的东西,我可以想象这可能是一个问题
c] 如果您 post 从 http
页面转到 https
页面,有时会删除 POST 数据(这可能是服务器-具体的,因为我觉得我有时看过这部作品,而不是其他作品)
d] 你有没有按照其他地方的建议得到 file_get_contents("php://input");
?它是否包含您的数据(看起来像这样:key1=value1&key2=value2
)还是太空白了?
e] 如果您在另一个页面上创建另一个表单并将其 POST 保存到另一个文件中,$_POST 数组会被填充吗?