Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php变量未识别索引_Php_Variables - Fatal编程技术网

php变量未识别索引

php变量未识别索引,php,variables,Php,Variables,我仍然是php的初学者,我似乎无法理解这里出了什么问题。即使存在“未识别索引”错误,代码仍然有效。我得到的错误是引用变量$food、$carries、$health、$submit 代码是: <?php require 'connect.inc.php'; $foodname = $_POST['food_name']; $calories = $_POST['calories']; $healthy = $_POST['healthy_unhealthy']; $submit_but

我仍然是php的初学者,我似乎无法理解这里出了什么问题。即使存在“未识别索引”错误,代码仍然有效。我得到的错误是引用变量$food、$carries、$health、$submit

代码是:

<?php

require 'connect.inc.php';

$foodname = $_POST['food_name'];
$calories = $_POST['calories'];
$healthy = $_POST['healthy_unhealthy'];
$submit_button = $_POST['submit'];

$sql="INSERT INTO `food` (`food`, `calories`, `healthy_unhealthy`) VALUES('$foodname', '$calories', '$healthy')";

if(isset($submit_button)&&!empty($foodname)&&!empty($calories)&&!empty($healthy))
{
    mysql_query($sql, $conn);
}
else{
echo'Kindly fill in fields';
}

?>
<form action="insert.php" method="POST">
Food Name:<br>
<input type="text" name="food_name"><br>
Calories:<br>
<input type="text" name="calories"><br>
Healthy:<br>
<input type="text" name="healthy_unhealthy"><br>
<input type="submit" name="submit">
</form>

首先,确保您处于接受数据的适当状态。将代码包装成:

<?php

require 'connect.inc.php';

// We only run this code if the user has POSTed data to this page. Without this we 
// will get an 'undefined index' error.
if(isset($_POST['submit'])) {
    $foodname = $_POST['food_name'];
    $calories = $_POST['calories'];
    $healthy = $_POST['healthy_unhealthy'];
    $submit_button = $_POST['submit'];

    $sql="INSERT INTO `food` (`food`, `calories`, `healthy_unhealthy`) VALUES('$foodname', '$calories', '$healthy')";

    if(isset($submit_button)&&!empty($foodname)&&!empty($calories)&&!empty($healthy))
    {
        mysql_query($sql, $conn);
    }
    else{
        echo'Kindly fill in fields';
    }
}

?>
<form action="insert.php" method="POST">
Food Name:<br>
<input type="text" name="food_name"><br>
Calories:<br>
<input type="text" name="calories"><br>
Healthy:<br>
<input type="text" name="healthy_unhealthy"><br>
<input type="submit" name="submit">
</form>
或者,你也可以做一个

var_dump($_POST);

这将打印出$\u POST变量的内容。如果您没有看到任何食品名称、卡路里或健康的参考,请检查您的表单是否正确,并将这些变量传递给web应用程序。

首先,请确保您处于接受数据的适当状态。将代码包装成:

<?php

require 'connect.inc.php';

// We only run this code if the user has POSTed data to this page. Without this we 
// will get an 'undefined index' error.
if(isset($_POST['submit'])) {
    $foodname = $_POST['food_name'];
    $calories = $_POST['calories'];
    $healthy = $_POST['healthy_unhealthy'];
    $submit_button = $_POST['submit'];

    $sql="INSERT INTO `food` (`food`, `calories`, `healthy_unhealthy`) VALUES('$foodname', '$calories', '$healthy')";

    if(isset($submit_button)&&!empty($foodname)&&!empty($calories)&&!empty($healthy))
    {
        mysql_query($sql, $conn);
    }
    else{
        echo'Kindly fill in fields';
    }
}

?>
<form action="insert.php" method="POST">
Food Name:<br>
<input type="text" name="food_name"><br>
Calories:<br>
<input type="text" name="calories"><br>
Healthy:<br>
<input type="text" name="healthy_unhealthy"><br>
<input type="submit" name="submit">
</form>
或者,你也可以做一个

var_dump($_POST);

这将打印出$\u POST变量的内容。如果您没有看到任何食品名称、卡路里或健康的参考,请检查您的表单是否正确,并将这些变量传递给web应用程序。

尝试在您的php中放入包装。。。像这样:

if (isset($_POST['submit'])) {
    //code here....
}
if (isset($_POST['food_name'])) {
    //do something here
}
$food_name = (isset($_POST['food_name'])) ? $_POST['food_name'] : '' ;
并将您的表格更改为:

<form action="" method="POST">

尝试在你的php中放入一个包装器。。。像这样:

if (isset($_POST['submit'])) {
    //code here....
}
if (isset($_POST['food_name'])) {
    //do something here
}
$food_name = (isset($_POST['food_name'])) ? $_POST['food_name'] : '' ;
并将您的表格更改为:

<form action="" method="POST">

第一次加载页面时,您将看到未定义的索引错误消息

要修复错误,请使用并检查表单是否已实际提交:

if(isset($_POST['submit'])) { 
print_r($_POST); //to see all the form inputs
// your code ...

}
我还要检查变量是否已设置:

$foodname = (isset($_POST['food_name'])) ? $_POST['food_name'] : NULL;
$calories = (isset($_POST['calories'])) ? $_POST['calories'] : NULL;
$healthy = (isset($_POST['healthy_unhealthy'])) ? $_POST['healthy_unhealthy'] : NULL;

不相关的旁注:您的代码容易受到SQL注入的攻击。不要直接在MySQL查询中插入变量,而是首先使用如下方法对其进行转义:

$foodname = mysql_real_escape_string($foodname);
$calories = mysql_real_escape_string($calories);
$healthy = mysql_real_escape_string($healthy);
这将有助于防止SQL注入。更好的是,停止使用
mysql.*
函数。它们不再得到维护。而是学习,并使用或

经过更正后,您的代码应该如下所示:

if(isset($_POST['submit'])) 
{
    /* form was submitted, proceed */

    $submit_button = $_POST['submit'];

    /* checking if user inputs are set */
    $foodname = (isset($_POST['food_name'])) ? $_POST['food_name'] : NULL;
    $calories = (isset($_POST['calories'])) ? $_POST['calories'] : NULL;
    $healthy = (isset($_POST['healthy_unhealthy'])) ? $_POST['healthy_unhealthy'] : NULL;

    /* escaping user inputs */
    $foodname = mysql_real_escape_string($foodname);
    $calories = mysql_real_escape_string($calories);
    $healthy = mysql_real_escape_string($healthy);

    //query
    $sql="INSERT INTO `food` (`food`, `calories`, `healthy_unhealthy`) VALUES('$foodname', '$calories', '$healthy')";

    /* storing query result to a variable */
    $result = mysql_query($sql, $conn);

    if($result) 
    { 
        //do stuff
    } 
    else 
    {
        die(mysql_error()); //display error, and exit
    }
}

希望这有帮助

首次加载页面时,您将看到未定义的索引错误消息

要修复错误,请使用并检查表单是否已实际提交:

if(isset($_POST['submit'])) { 
print_r($_POST); //to see all the form inputs
// your code ...

}
我还要检查变量是否已设置:

$foodname = (isset($_POST['food_name'])) ? $_POST['food_name'] : NULL;
$calories = (isset($_POST['calories'])) ? $_POST['calories'] : NULL;
$healthy = (isset($_POST['healthy_unhealthy'])) ? $_POST['healthy_unhealthy'] : NULL;

不相关的旁注:您的代码容易受到SQL注入的攻击。不要直接在MySQL查询中插入变量,而是首先使用如下方法对其进行转义:

$foodname = mysql_real_escape_string($foodname);
$calories = mysql_real_escape_string($calories);
$healthy = mysql_real_escape_string($healthy);
这将有助于防止SQL注入。更好的是,停止使用
mysql.*
函数。它们不再得到维护。而是学习,并使用或

经过更正后,您的代码应该如下所示:

if(isset($_POST['submit'])) 
{
    /* form was submitted, proceed */

    $submit_button = $_POST['submit'];

    /* checking if user inputs are set */
    $foodname = (isset($_POST['food_name'])) ? $_POST['food_name'] : NULL;
    $calories = (isset($_POST['calories'])) ? $_POST['calories'] : NULL;
    $healthy = (isset($_POST['healthy_unhealthy'])) ? $_POST['healthy_unhealthy'] : NULL;

    /* escaping user inputs */
    $foodname = mysql_real_escape_string($foodname);
    $calories = mysql_real_escape_string($calories);
    $healthy = mysql_real_escape_string($healthy);

    //query
    $sql="INSERT INTO `food` (`food`, `calories`, `healthy_unhealthy`) VALUES('$foodname', '$calories', '$healthy')";

    /* storing query result to a variable */
    $result = mysql_query($sql, $conn);

    if($result) 
    { 
        //do stuff
    } 
    else 
    {
        die(mysql_error()); //display error, and exit
    }
}

希望这有帮助

PHP允许您使用未定义或未声明的变量。当您引用一个从未声明过的变量时,您会收到此通知

当遇到一个未识别的变量时,它会将默认的“零”值作为扣除类型。数字的大小写为0,字符串的大小写为空字符串

在您的情况下,
$\u POST
变量没有用值填充(它们是通过发布表单填充的),您会收到每个未识别变量的通知

更多信息请参见:

在PHP中不需要初始化变量,但这是一个非常好的实践。未初始化的变量具有其类型的默认值,具体取决于使用它们的上下文-布尔值默认为FALSE,整数和浮点值默认为零,字符串(例如在echo中使用)设置为空字符串,数组变为空数组


无论这是否是一个明智的语言设计决策,我将留给您自己。

PHP允许您使用未定义或未声明的变量。当您引用一个从未声明过的变量时,您会收到此通知

当遇到一个未识别的变量时,它会将默认的“零”值作为扣除类型。数字的大小写为0,字符串的大小写为空字符串

在您的情况下,
$\u POST
变量没有用值填充(它们是通过发布表单填充的),您会收到每个未识别变量的通知

更多信息请参见:

在PHP中不需要初始化变量,但这是一个非常好的实践。未初始化的变量具有其类型的默认值,具体取决于使用它们的上下文-布尔值默认为FALSE,整数和浮点值默认为零,字符串(例如在echo中使用)设置为空字符串,数组变为空数组

无论这是否是一个聪明的语言设计决策,我将留给你自己。

你有这个代码

$sql="INSERT INTO `food` (`food`, `calories`, `healthy_unhealthy`) VALUES('$foodname', '$calories', '$healthy')";

if(isset($submit_button)&&!empty($foodname)&&!empty($calories)&&!empty($healthy))
{
    mysql_query($sql, $conn);
}
其中,您正在使用
if
语句之外的变量,而这些变量第一次不可用,因此,您可以使用,(
$sql
变量应填充在if语句中)

此外,您可以使用(更好)

你有这个密码吗

$sql="INSERT INTO `food` (`food`, `calories`, `healthy_unhealthy`) VALUES('$foodname', '$calories', '$healthy')";

if(isset($submit_button)&&!empty($foodname)&&!empty($calories)&&!empty($healthy))
{
    mysql_query($sql, $conn);
}
其中,您正在使用
if
语句之外的变量,而这些变量第一次不可用,因此,您可以使用,(
$sql
变量应填充在if语句中)

此外,您可以使用(更好)


未识别索引
不是错误,而是通知。它不会停止您的脚本,因此它将继续

请记住您的
$\u POST
-变量可能为空,这就是导致出现此通知的原因。这本身不是问题,但您可以在sql语句中继续使用它(现在没有正确初始化变量)——如果处理不当,可能会产生问题

出于调试目的,执行快速
var\u转储($\u POST)以查看其中的内容

您可以使用如下函数来避免这种情况:

if (isset($_POST['submit'])) {
    //code here....
}
if (isset($_POST['food_name'])) {
    //do something here
}
$food_name = (isset($_POST['food_name'])) ? $_POST['food_name'] : '' ;
我倾向于尽可能地初始化变量,例如:

if (isset($_POST['submit'])) {
    //code here....
}
if (isset($_POST['food_name'])) {
    //do something here
}
$food_name = (isset($_POST['food_name'])) ? $_POST['food_name'] : '' ;
这导致了一个更麻烦的问题:SQL安全性。这样编写的脚本很容易受到sql注入攻击

关于这个话题有两条建议:

  • 不要使用mysql扩展。它已被弃用、使用或替代
  • 在使用变量之前对其进行清理(例如使用or、ev