dongzheng8463 2013-09-16 19:14
浏览 35
已采纳

php变量unidentified index [重复]

I'm still a beginner to php and I cannot seem to understand what's wrong here. The code still works even though there's an 'unidentified index' error. The error I get would be referring to the variables $food, $calories, $healthy, $submit.

The code is:

<?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>
</div>
  • 写回答

6条回答 默认 最新

  • dsutuyxe088689 2013-09-16 19:18
    关注

    First and foremost, ensure that you're in the appropriate state to be accepting the data. Wrap your code in an:

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

    This will ensure that you're currently receiving a POST request, from the form you've defined.

    The $_POST variable is an array containing data you've sent to the web application via a POST request. In your form you should have fields with the appropriate names (food_name, calories, healthy_unhealthy, etc). It sounds like these fields may be missing.

    In your code, somewhere near the top, put in the following:

    print_r($_POST);
    

    or, alternatively you could do a

    var_dump($_POST);
    

    This will print out the contents of your $_POST variable. If you do not see any reference to food_name, calories, or healthy_unhealthy check that your form is correct and is passing these variables to the web application.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢