关于XXX.php?id=,XXX.php中的变量id可用的条件是什么?解决办法
关于XXX.php?id=,XXX.php中的变量id可用的条件是什么?
初学php,遇到很多问题,这个问题实在暂时没想明白,希望大家帮看看。
<br /><?php <br /> include("conn.php"); <br /> if(!empty($_GET['id']))<br /> {<br /> $id=$_GET['id'];<br /> $sql="select * from `user` where `id`='$id'";<br /> $query=mysql_query($sql);<br /> $result=mysql_fetch_array($query);<br /> } <br /> if(!empty($_POST['sub']))<br /> {<br /> //$id=$_GET['id'];<br /> $title=$_POST['title'];<br /> $content=$_POST['content'];<br /> $id=$_POST['hid'];<br /> $sql="update `user` set `title` = '$title', `content` = '$content' where id='$id limit 1'";<br /> mysql_query($sql);<br /> echo "<script> alert('更新完成'); location.href='index.php'</script>";<br /> }<br />?><br /><form action="edit.php" method="post"><br /><input type="hidden" name="hid" value="<?php echo $result['id'] ?>"><br><br />标题<input type="text" name="title" value="<?php echo $result['title'] ?>"><br><br />内容<textarea rows="5" cols="50" name="content"><?php echo $result['content'] ?></textarea><br><br /><input type="submit" name="sub" value="提交"><br /></form><br /><br />
刚看了php100的视频,讲解关于微型博客。其中实现博客的编辑功能,在index.php中放入一个编辑的链接,如下:
<br /><h2 id="php-nbsp-echo-nbsp-result-title-nbsp-br-a-nbsp-href-nbsp-nbsp-edit-php-id-php-nbsp-echo-nbsp-result-id-编辑-a-nbsp-br-a-nbsp-href-nbsp-nbsp-delete-php-id-php-nbsp-echo-nbsp-result-id-删除-a"><?php echo $result['title']; ?><br /> |<a href = "edit.php?id=<?php echo $result['id'];?>">编辑</a> <br /> <a href = "delete.php?id=<?php echo $result['id'];?>">删除</a>|</h2><br />
问题是,为什么当我在edit.php中的第二个if里不能直接用这个id调用(如代码中被注释的一行,如果这样,会报Undefined index id而且编辑不生效),而是要在表单里再重新插入hidden?
------解决方案--------------------
编辑
这个应该是列表页面进入编辑页面的url,使用 是用$_GET 获取的,代码没错。
然后进入到编辑页面,修改内容后,你按submit提交。表单的method是$_POST。所以$_GET['id']当然没有数据,因为要用$_POST获取。但你加了hidden,并用$id=$_POST['hid'];获取到了。
<br /> if(!empty($_POST['sub']))<br /> {<br /> //$id=$_GET['id'];<br /> $title=$_POST['title'];<br /> $content=$_POST['content'];<br /> $id=$_POST['hid'];<br /> $sql="update `user` set `title` = '$title', `content` = '$content' where id='$id limit 1'";<br /> mysql_query($sql);<br /> echo "<script> alert('更新完成'); location.href='index.php'</script>";<br /> }<br />
代码可以这样优化:
<br />include("conn.php"); <br /><br />if(isset($_POST['sub'])){ // 判斷是否提交表單<br /> $title=$_POST['title'];<br /> $content=$_POST['content'];<br /> $id=$_POST['hid'];<br /> $sql="update `user` set `title` = '$title', `content` = '$content' where id='$id limit 1'";<br /> mysql_query($sql);<br /> echo "<script> alert('更新完成'); location.href='index.php'</script>";<br /> exit();<br />}else{ // 不是提交表單,表示是從列表頁過來<br /> $id=$_GET['id'];<br /> $sql="select * from `user` where `id`='$id'";<br /> $query=mysql_query($sql);<br /> $result=mysql_fetch_array($query);<br />}<br />?><br /><br /><form action="edit.php" method="post"><br /><input type="hidden" name="hid" value="<?php echo $result['id'] ?>"><br><br />标题<input type="text" name="title" value="<?php echo $result['title'] ?>"><br><br />内容<textarea rows="5" cols="50" name="content"><?php echo $result['content'] ?></textarea><br><br /><input type="submit" name="sub" value="提交"><br /></form><br />
------解决方案--------------------
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 造成修改失败。还会给出'更新完成'的虚假信息

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
