What are the available conditions for variable IDs in xxx.php?id=,xxx.php?
Beginners PHP, encountered a lot of problems, this problem is not really want to understand, I hope you can help to see.
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 "";
}
?>
Just watched Php100 's video, explaining about micro blogs. Which realizes the editing function of the blog, put an edit link in index.php, as follows:
"> Delete |
The question is, why should I not call it directly with this ID in the second if in edit.php (such as the line of comments in the code, if so, will be reported undefined index ID and the edit does not take effect), but in the form and then re-insert hidden?
------Solution--------------------
"> Edit
This should be the URL of the list page into the edit page, use is obtained by $_get, the code is correct.
Then go to the edit page, and after you modify the content, you press submit. The method of the form is $_post. So $_get[' id ', of course, has no data, because it is to be obtained with $_post. But you add the hidden and use the $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 "";
}
The code can be optimized like this:
Include ("conn.php");
if (Isset ($_post[' Sub '))) {//Determine whether the form is submitted
$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 "";
Exit ();
}else{//Not submitting a form, indicating that it was from the list page
$id =$_get[' id '];
$sql = "SELECT * from ' user ' where ' id ' = ' $id '";
$query =mysql_query ($sql);
$result =mysql_fetch_array ($query);
}
?>
------Solution--------------------
Edit.php has two different features that will be executed twice during the modification of the Weibo
First time: edit.php?id=nnn
There are URL parameters, program walk if (!empty ($_get[' id ')) True Branch
Complete query data
and filling the form
Second form submission edit.php
No URL parameter, program walk if (!empty ($_post[' Sub ')) True Branch
Jump to catalog page when data modification is complete
$id =$_get[' id '] is not available in the second if the two conditions are not set up at the same time
There is a potential problem with this program:
When a direct browser accesses edit.php, the two conditions are not true. An error occurred while filling the form, exposing the server layout
If error checking is ignored, this should be the new Weibo entry. But edit.php doesn't have any new micro-blog processing code.
And as a result of the change, the lack of ID caused by the modification failed. It also gives false information about ' update complete '.