为什么我有这个“;未定义的索引错误:id在..中&”;


Why do I have this "Undefined Index error: id in ..."?

注意:未定义的索引:134 行C:''wamp''www''biding''view''ctgryedit.php中的id

我在做编辑操作时收到了这个通知。我给出的代码是

     <?php
     $cid=$_GET['id'];
$con=mysql_connect('localhost','root','')or die("error in connection");
mysql_select_db('bidding',$con) or die("error in db");
$sql="select * from category where `catgry_id`='$cid'";
$res=mysql_query($sql,$con);
$row=mysql_fetch_array($res); 
     ?>

正在使用查询字符串发送值。。。。请任何人帮我解决错误。。。

在盲目使用数组索引之前,需要检查数组索引。试试之类的东西

$cid = isset($_GET['id']) ? $_GET['id'] : null; // or some other sane default

关于脚本失败的原因,您确定是通过HTTP调用您的脚本(或包含您的脚本的脚本),查询字符串中包含?id=something吗?

我想到了两种可能性:

  1. 打字错误:也许您拼写错误了参数名称,或者它是通过POST而不是错误的GET发送的,或者
  2. 参数列表为空,因为您直接调用脚本ctgryedit.php而不是ctgryeedit.php?id=foo

在第二种情况下,您可以通过以下方式检查是否有任何GET数据:

if ($_GET) {
    //Your code
}
else {
    //File has been called directly without any parameter
}

这听起来可能很荒谬,但这些可能性是你能找到的最常见的错误,也是程序员最后想到的。

您正在使用GET方法检查你的网址,它应该像

localhost/pageName.php?id=5

如果没有,你的表格有问题检查您的表单方法是否为"GET",并且您已经为id 指定了name属性

<form method='get' id='thisForm'>
<input type='text' name='id' />
<input type='submit' value="Submit" />
</form>

如果id显示在URL 中

然后

 if(isset($_GET['id']))
 {
     $cid=$_GET['id'];
     // SQL query
 }