douzhang8033 2012-03-28 23:49
浏览 13
已采纳

未定义的索引通知

I have spent days trying to figure this out to no avail. I have checked the PHP docs as well as multiple other form questions regarding the "undefined index" notice and what seems to be the usual solution "isset()" function.

But I cannot work out the solution in my case.

Here is the code:

<form action="" method="post">
<!-- Here are all the form fields -->
<input type="submit" name="send" value="send"/>
</form>

<?php

            $con = mysql_connect("#","#","#");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }

            mysql_select_db("#", $con);

            $sql="
            INSERT INTO events (e_date, e_time, e_type, e_name)
            VALUES ('$_POST[form_e_date]','$_POST[form_e_time]','$_POST[form_e_type]','$_POST[form_e_name]')";

            if (!mysql_query($sql,$con))
            {
                die('Error: ' . mysql_error());
            }
            echo "1 record added";

            mysql_close($con)

        ?>

The 4 notices occur on the line with the $sql variable starting at "VALUES" and refer to the form_e_date, etc.

I apologize for asking a question that I realize gets somewhat frequently, but I have tried other people's solutions which either do no work for me or else I am not implementing them correctly.

Please help, and please feel free to leave any other constructive feedback about my code as I'm a newbie.

Thanks!

  • 写回答

1条回答 默认 最新

  • donglugou6652 2012-03-28 23:52
    关注

    instead of

    '$_POST[form_e_date]','$_POST[form_e_time]','$_POST[form_e_type]','$_POST[form_e_name]'
    

    you should NOT use

    '$_POST["form_e_date"]','$_POST["form_e_time"]','$_POST["form_e_type"]','$_POST["form_e_name"]'
    

    because of the danger of SQL injection attacks.

    you should be using mysql_real_escape_string to protect yourself against this.

    this would be much safer:

    if (array_key_exists("form_e_date", $_POST))
      $form_e_date = mysql_real_escape_string($_POST["form_e_date"]);
    else
      $form_e_date = null;
    
    if (array_key_exists("form_e_time", $_POST))
      $form_e_time = mysql_real_escape_string($_POST["form_e_time"]);
    else
      $form_e_time = null;
    
    if (array_key_exists("form_e_type", $_POST))
      $form_e_type = mysql_real_escape_string($_POST["form_e_type"]);
    else
      $form_e_type = null;
    
    if (array_key_exists("form_e_name", $_POST))
      $form_e_name = mysql_real_escape_string($_POST["form_e_name"]);
    else
      $form_e_name = null;
    .
    .
    .
    $sql = "INSERT INTO events (e_date, e_time, e_type, e_name) " . 
      "VALUES ('{$form_e_date}','{$form_e_time}','{$form_e_type}','{$form_e_name}')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来