About XXX. php? Id =, what are the available conditions for variable id in XXX. php?

Source: Internet
Author: User
About XXX. php? Id. What are the available conditions for variable id in XXX. php? I have encountered many problems when I was a beginner in php.
 


I just watched the php100 video and explained about the micro-blog. To edit a blog, add an edit link to index. php, as shown below:
 | "> Edit"> delete |

The problem is, why when I am in edit. the second if in php cannot be called directly with this id (for example, the comment line in the code. if so, the Undefined index id will be reported and the editing will not take effect ), instead, we need to re-insert the hidden in the form?


Reply to discussion (solution)

"> Edit
This should be the url of the list page to enter the editing page. it is obtained using $ _ GET, and the code is correct.

Then go to the edit page. after the content is modified, you can submit it by submit. The form method is $ _ POST. Therefore, $ _ GET ['id'] certainly does not have data, because it must be obtained using $ _ POST. But you added hidden and obtained it with $ 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 ('update finished '); location. href = 'index. php 'script ';}



The code can be optimized as follows:
Include ("conn. php"); if (isset ($ _ POST ['sub']) {// judge? Submit a table? $ 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 ('update finished '); location. href = 'index. php 'script "; exit ();} else {// not a table to be submitted ?, Indicates yes? List ??? $ Id = $ _ GET ['id']; $ SQL = "select * from 'user' where 'id' = '$ ID '"; $ query = mysql_query ($ SQL); $ result = mysql_fetch_array ($ query) ;}?>

Edit. php has two different functions. it will be executed twice during Weibo modification.
First time: edit. php? Id = nnn
If (! Empty ($ _ GET ['id']) true Branch
Complete data query
And fill the form
Second form submission edit. php
If (! Empty ($ _ POST ['sub']) true Branch
After the data is modified, go to the directory page.

Because the two conditions are not true at the same time, $ id = $ _ GET ['id'] is not available in the second if

This program has a potential problem:
When you directly access edit. php through a browser, neither of the two conditions is true. An error occurs when filling the form, exposing the server layout.
If the error check is ignored, enter the new Weibo account. However, no processing code is added for edit. php.
If the id is missing, the modification fails. False information about 'update complete' is also provided.

Thank you for your answers!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.