未定义变量:POST - PHP 和 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11520795/
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 00:43:26  来源:igfitidea点击:

Undefined variable: POST - PHP and MySQL

phpmysql

提问by Tobias Moe Thorstensen

I've got a form like this:

我有一个这样的表格:

<form name="htmlform" method="post" action="script/gen.php?postData">
            <table width="450px">
                </tr>
                <tr>
                    <td valign="top">
                        <label for="customer">Customer:</label>
                    </td>
                    <td valign="top">
                        <input  type="text" name="customer" maxlength="50" size="30">
                    </td>
                </tr>

                <tr>
                    <td valign="top"">
                        <label for="nol">Number of licences: </label>
                    </td>
                    <td valign="top">
                        <input type="text" name="nol" maxlength="50" size="30">
                    </td>
                </tr>

                <tr>
                    <td>
                        <form method="post" id="submit" action="script/gen.php">
                            <input type="button" onClick="getKey()"; value="Generate key"/>
                    </td>
                </tr>



                <div id="innhold">
                            <h4>Licence Key: </h>
                </div>

                <tr>
                    <td colspan="2" style="text-align:center">
                        <input type="submit" value="Submit"> 
                    </td>
                </tr>

                </table>
            </form>

The codelines of interest is:

感兴趣的代码行是:

<input type="text" name="nol" maxlength="50" size="30">

and

<input  type="text" name="customer" maxlength="50" size="30">

I try to write this information to a database like this:

我尝试将此信息写入这样的数据库:

function postData($key1) {
//just to check if the key is equal to the one thats posted to the user
//echo '<h5>From postData' . $key1 . '</h5>';
/*echo '<script type="text/javascript"> alert("The order has been submitted successfully");
location = "/Webpanel/index.html";
</script>';*/

$customerVar = $POST['customer'];
$nolVar = $POST['nol'];

mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("licencedatabase");

$query_add = "INSERT INTO licence (`customer_name`,`licence_count`) VALUES ('$customerVar','$nolVar')";
$query_exec = mysql_query($query_add) or die(mysql_error()); 
mysql_close();
}

But I keep getting the error:

但我不断收到错误消息:

Undefined variable: POST

How can I accomplish this? Thanks in advance.

我怎样才能做到这一点?提前致谢。

回答by abhshkdz

Its $_POSTnot $POST.

它的$_POST不是$POST

$customerVar = $_POST['customer'];
$nolVar = $_POST['nol'];

回答by Shahzeb

Try using $_POSTinstead of $POST

尝试使用$_POST代替$POST

回答by deceze

That's because it's called $_POST.

那是因为它被称为$_POST.

回答by Gntem

to access the superglobal POST use $_POSTnot $POST

访问超全局 POST 使用$_POST$POST

回答by Edd Slipszenko

All PHP superglobals (such as those for GET and POST) are prefixed with an underscore, so: $POSTshould be $_POST.

所有 PHP 超全局变量(例如 GET 和 POST 的那些)都以下划线为前缀,因此:$POST应该是$_POST.

Have a look here for more information about the available superglobals in PHP: http://php.net/manual/en/language.variables.superglobals.php

在此处查看有关 PHP 中可用超全局变量的更多信息:http: //php.net/manual/en/language.variables.superglobals.php

回答by Edd Slipszenko

Try using $_POSTinstead of $POST

尝试使用$_POST代替$POST

Check following example:

检查以下示例:

The predefined $_POSTvariable is used to collect values from a form sent with method="post".

预定义$_POST变量用于从发送的表单中收集值method="post"

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

使用 POST 方法从表单发送的信息对其他人是不可见的,并且对要发送的信息量没有限制。

Example:

例子:

<form action="submitform.php" method="post">
    Name: <input type="text" name="fname" />
    Age:  <input type="text" name="age" />
    <input type="submit" />
</form>

When the user clicks the "Submit"button, the URL will look like this: http://localhost/submitform.php

当用户单击"Submit"按钮时,URL 将如下所示:http://localhost/submitform.php

The "submitform.php"file can now use the $_POSTvariable to collect form data (the names of the form fields will automatically be the keys in the $_POSTarray):

"submitform.php"文件现在可以使用该$_POST变量来收集表单数据(表单字段的名称将自动成为$_POST数组中的键):

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?>  years old. 

might you will understand clearly.

或许你会明白。