1

我仍然是 php 的初学者,我似乎无法理解这里出了什么问题。即使存在“未识别的索引”错误,该代码仍然有效。我得到的错误是指变量 $food、$calories、$healthy、$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>
4

6 回答 6

3

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

<?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>

这将确保您当前正在接收来自您定义的表单的 POST 请求。

$_POST 变量是一个数组,其中包含您通过 POST 请求发送到 Web 应用程序的数据。在您的表单中,您应该有具有适当名称的字段(food_name、calories、healthy_unhealthy 等)。听起来这些字段可能会丢失。

在您的代码中,靠近顶部的某处,输入以下内容:

print_r($_POST);

或者,或者你可以做一个

var_dump($_POST);

这将打印出 $_POST 变量的内容。如果您没有看到对 food_name、calories 或 health_unhealthy 的任何引用,请检查您的表单是否正确并将这些变量传递给 Web 应用程序。

于 2013-09-16T19:18:19.783 回答
2

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

if (isset($_POST['submit'])) {
    //code here....
}

并将您的形式更改为:

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

调试..使用这个:

var_dump($toDebug);
于 2013-09-16T19:18:52.103 回答
1

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

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

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 查询中插入变量,而是先使用 转义它们mysql_real_escape_string(),如下所示:

$foodname = mysql_real_escape_string($foodname);
$calories = mysql_real_escape_string($calories);
$healthy = mysql_real_escape_string($healthy);

这将有助于防止 SQL 注入。更好的是,停止使用这些mysql_*功能。它们不再被维护并被正式弃用。改为了解准备好的语句,并使用PDOMySQLi

通过更正,您的代码应如下所示:

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
    }
}

希望这可以帮助!

于 2013-09-16T19:18:57.010 回答
1

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

当遇到未识别的变量时,它采用扣除类型的默认“零”值。0 表示数字,或空字符串表示字符串。

在您的情况下,$_POST变量没有填充值(它们是通过发布表单来填充的),并且您会收到每个未识别变量的通知。

更多可以在文档中找到:

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

无论这是否是一个聪明的语言设计决策,我都会自己决定。

于 2013-09-16T19:22:23.617 回答
0

你有这个代码

$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 语句中

if( isset($_POST['submit']) && (!empty($foodname) && !empty($calories) &&!empty($healthy)) ) { 
    $sql="INSERT INTO `food` (`food`, `calories`, `healthy_unhealthy`) VALUES('$foodname', '$calories', '$healthy')";
    mysql_query($sql, $conn);
}

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

if( isset($_POST['submit']) ) { 
    $foodname = $_POST['food_name'];
    $calories = $_POST['calories'];
    $healthy = $_POST['healthy_unhealthy'];
    if( !empty($foodname) && !empty($calories) && !empty($healthy) ){
        $sql="INSERT INTO `food` (`food`, `calories`, `healthy_unhealthy`) VALUES('$foodname', '$calories', '$healthy')";
        mysql_query($sql, $conn);
    }
}
于 2013-09-16T19:24:03.850 回答
0

Unindentified index不是错误,是通知。它不会停止您的脚本,因此它会继续。

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

出于调试目的,请快速var_dump($_POST);查看其中的内容。

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

if (isset($_POST['food_name'])) {
    //do something here
}

我倾向于尽可能地初始化我的变量,就像这样:

$food_name = (isset($_POST['food_name'])) ? $_POST['food_name'] : '' ;

这导致了一个更成问题的问题:SQL 安全性。像这样编码,您的脚本很容易受到 sql 注入攻击。

关于这个主题的两个建议:

  • 不要使用 mysql 扩展。它已被弃用,请改用mysqlipdo:mysql
  • 在使用变量之前对其进行清理(pe using mysqli_real_escape_string,或者更好的是,使用准备好的语句。
于 2013-09-16T19:26:49.020 回答