关于XXX.php?id=,XXX.php中的变量id可用的条件是什么?解决办法

关于XXX.php?id=,XXX.php中的变量id可用的条件是什么?
初学php,遇到很多问题,这个问题实在暂时没想明白,希望大家帮看看。

<?php 
include("conn.php");
if(!empty($_GET['id']))
{
$id=$_GET['id'];
$sql="select * from `user` where `id`='$id'";
$query=mysql_query($sql);
$result=mysql_fetch_array($query);
}
if(!empty($_POST['sub']))
{
//$id=$_GET['id'];
$title=$_POST['title'];
$content=$_POST['content'];
$id=$_POST['hid'];
$sql="update `user` set `title` = '$title', `content` = '$content' where id='$id limit 1'";
mysql_query($sql);
echo "<script> alert('更新完成'); location.href='index.php'</script>";
}
?>
<form action="edit.php" method="post">
<input type="hidden" name="hid" value="<?php echo $result['id'] ?>"><br>
标题<input type="text" name="title" value="<?php echo $result['title'] ?>"><br>
内容<textarea rows="5" cols="50" name="content"><?php echo $result['content'] ?></textarea><br>
<input type="submit" name="sub" value="提交">
</form>



刚看了php100的视频,讲解关于微型博客。其中实现博客的编辑功能,在index.php中放入一个编辑的链接,如下:

<h2><?php echo $result['title']; ?>
|<a href = "edit.php?id=<?php echo $result['id'];?>">编辑</a> 
<a href = "delete.php?id=<?php echo $result['id'];?>">删除</a>|</h2>

问题是,为什么当我在edit.php中的第二个if里不能直接用这个id调用(如代码中被注释的一行,如果这样,会报Undefined index id而且编辑不生效),而是要在表单里再重新插入hidden?

------解决方案--------------------
<a href = "edit.php?id=<?php echo $result['id'];?>">编辑</a>
这个应该是列表页面进入编辑页面的url,使用<a href="xxx"> 是用$_GET 获取的,代码没错。

然后进入到编辑页面,修改内容后,你按submit提交。表单的method是$_POST。所以$_GET['id']当然没有数据,因为要用$_POST获取。但你加了hidden,并用$id=$_POST['hid'];获取到了。

 if(!empty($_POST['sub']))
    {
        //$id=$_GET['id'];
        $title=$_POST['title'];
        $content=$_POST['content'];
        $id=$_POST['hid'];
        $sql="update `user` set `title` = '$title', `content` = '$content' where id='$id limit 1'";
        mysql_query($sql);
        echo "<script> alert('更新完成'); location.href='index.php'</script>";
    }



代码可以这样优化:

include("conn.php");   

if(isset($_POST['sub'])){ // 判斷是否提交表單
    $title=$_POST['title'];
    $content=$_POST['content'];
    $id=$_POST['hid'];
    $sql="update `user` set `title` = '$title', `content` = '$content' where id='$id limit 1'";
    mysql_query($sql);
    echo "<script> alert('更新完成'); location.href='index.php'</script>";
    exit();
}else{ // 不是提交表單,表示是從列表頁過來
    $id=$_GET['id'];
    $sql="select * from `user` where `id`='$id'";
    $query=mysql_query($sql);
    $result=mysql_fetch_array($query);
}
?>

<form action="edit.php" method="post">
<input type="hidden" name="hid" value="<?php echo $result['id'] ?>"><br>
标题<input type="text" name="title" value="<?php echo $result['title'] ?>"><br>
内容<textarea rows="5" cols="50" name="content"><?php echo $result['content'] ?></textarea><br>
<input type="submit" name="sub" value="提交">
</form>

------解决方案--------------------
edit.php 具有两个不同的功能,在修改微博的过程中将执行两次
第一次:edit.php?id=nnn 
有 url 参数,程序走 if(!empty($_GET['id'])) 真 分支
完成查询数据
并充填表单
第二次 表单提交 edit.php
没有 url 参数,程序走 if(!empty($_POST['sub'])) 真 分支
完成数据修改后跳转至目录页

由于两个条件不会同时成立,所以 $id=$_GET['id'] 在第二个 if 中不可用

此程序有一个潜在的问题:
当直接浏览器访问 edit.php 时,因为两个条件都不成立。会在充填表单时发生错误,而暴露服务器布局
如果忽略错误检查,此时应为新微博输入。但 edit.php 并无新增微博的处理代码
而当做修改的话,又因缺少 id 造成修改失败。还会给出'更新完成'的虚假信息