du512926 2016-06-03 04:54
浏览 37
已采纳

未定义的索引ID .. PHP [重复]

I'm trying to delete some data from a table in a mysqli database, and I receive that error. I tried with isset, writing it directly without using a variable, nothing works. What am I doing wrong?

<?php

echo"hello";

$link = mysqli_connect("localhost", "root", "", "documents");

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}


$ID = $_POST['id'];

$result =  mysqli_query($link, "DELETE FROM document WHERE id='$ID'");


header("Location: connect-db.php");
?>
</div>
  • 写回答

1条回答 默认 最新

  • dstnlhhv791576 2016-06-03 05:08
    关注

    If you are fetching id from URL then use

    $ID =isset( $_REQUEST['id']) ? $_REQUEST['id'] : 0; 
    

    OR

    $ID =isset( $_GET['id']) ? $_GET['id'] : 0;
    

    If you are fetching id from POST variables then use

    $ID =isset( $_POST['id']) ? $_POST['id'] : 0;
    

    $ _REQUEST , $ _POST or $ _GET are special functions of PHP that are used to get variables from a user-filled form. While using these functions, a user may encounter an error - Notice: Undefined index. This error can be avoided with the help of PHP isset (). so you just have to check if the index exists or not before you use them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!