PHP 将所有 $_POST[] 变量访问到一个数组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5345859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:05:58  来源:igfitidea点击:

PHP access all $_POST[] variables into an array?

phparraysooppost

提问by Aaron

how to capture all fields in the $_POSTVARIABLE? into an array?

如何捕获$_POSTVARIABLE中的所有字段?成一个数组?

$email = $_POST;
$emails = array_keys($email);
foreach($emails as $email) {
        echo '$' . nl2br($email);
}
$emailadd = new email_save;
$emailadd->insert_email_into_database("POST VARIABLES GO HERE)

I'm trying to make an email list, name and email address, how do I capture all the variables that where posted that can normal be accessed like $_POST['email']into an array so i can add them to the arguments in my functions?

我正在尝试制作一个电子邮件列表、姓名和电子邮件地址,我如何捕获所有可以正常访问的变量,例如 $_POST['email']数组,以便我可以将它们添加到我的函数中的参数中?

My form field has 5 fields. The method is POST.

我的表单字段有 5 个字段。方法是POST。

Instead of writing,

而不是写作,

$email = mysql_real_escape_string($_POST['email']);
$firstname = mysql_real_escape_string($_POST['firstname']);

I'm trying to access all the values in the $_POST[] array; So I can save time getting posted variables and add functions to those variables like mysql_real_escape_string();

我正在尝试访问 $_POST[] 数组中的所有值;因此,我可以节省获取已发布变量的时间并将函数添加到这些变量中,例如mysql_real_escape_string();

Other security measures will be figured out.

将想出其他安全措施。

I want to be able to add a reffrence of the array to a function so I can add them to the database.

我希望能够将数组的引用添加到函数中,以便将它们添加到数据库中。

采纳答案by mario

If you want to capture a list from a POSTed form, then use the array syntax trick instead of enumerated input field names:

如果要从 POST 表单中捕获列表,请使用数组语法技巧而不是枚举输入字段名称:

<input type="email" name="emails[]">
<input type="email" name="emails[]">
<input type="email" name="emails[]">

This way you need no guessing in PHP, because emails[]becomes an array implicitely then:

这样你就不需要在 PHP 中猜测了,因为这样emails[]就隐式地变成了一个数组:

print_r($_POST["emails"]);
foreach ($_POST["emails"] as $email) {

For database-escaping just use:

对于数据库转义,只需使用:

$db_emails = array_map("mysql_real_escape_string", $_POST["emails"]);
// that's an array too

回答by SimplyZ

$_POST is already an array. Why not just pass that?

$_POST 已经是一个数组。为什么不直接通过呢?

回答by Jeremy Conley

Hmm...would something like this help?

嗯……这样的事情会有帮助吗?

<?php
// blank array to hold emails
$emails = array();

foreach ( $_POST as $key => $value )
{
    if ( preg_match('/email/', $key) )
    {
        $this_email = $value;
        // quick check to see if it is valid
        $this_email = filter_var($this_email, FILTER_VALIDATE_EMAIL);
        // also escape it to prevent SQL injections
        $this_email = $mysqli->real_escape_string($this_email);
        $emails[] = $this_email;
    }
}

// $emails is now a nice, clean array of email addresses
$emailadd->insert_email_into_database($emails);

echo '<pre>';
print_r($emails);
echo '</pre>';
?>

<form method="post" action="index.php">
    <input type="text" name="email1" /><br />
    <input type="text" name="email2" /><br />
    <input type="text" name="email3" /><br />
    <input type="submit" />
</form>