为什么我有这个“Undefined Index error:id in ...”?

时间:2012-03-15 03:18:32

标签: php

注意:第134行的C:\ wamp \ www \ bidding \ 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); 
     ?>

我使用查询字符串发送值.... Plz任何人帮我解决错误...

3 个答案:

答案 0 :(得分:1)

您需要在盲目使用之前检查数组索引。尝试像

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

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

答案 1 :(得分:0)

我想到了2个可能性:

  1. 一个错字:也许你拼错了参数名称,或者它是通过POST而不是GET发送错误或
  2. 参数列表为空,因为您直接调用脚本ctgryedit.php而不是ctgryedit.php?id = foo
  3. 在第二种情况下,您可以通过以下方式检查是否有任何GET数据:

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

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

答案 2 :(得分:0)

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

localhost/pageName.php?id=5

如果没有,您的表单有问题 检查您的表单方法是否为&#39; GET&#39;和 您已为id

指定了name属性
<form method='get' id='thisForm'>
<input type='text' name='id' />
<input type='submit' value="Submit" />
</form>

如果ID显示在网址

然后

 if(isset($_GET['id']))
 {
     $cid=$_GET['id'];

     // SQL query
 }