PHP checks whether there are null values in the form. Why does PHP not work?
If (intval ($ _ GET ['subj']) = 0 ){
Redirect_to ("content. php ");
}
If (isset ($ _ POST ['submit ']) {
$ Errors = array ();
$ Required_fields = array ('menu _ name', 'position', 'visable ');
Foreach ($ required_fields as $ fieldname ){
If (! Isset ($ _ POST [$ fieldname]) | (empty ($ _ POST [$ fieldname]) & $ _ POST [$ fieldname]! = 0 )){
$ Errors [] = $ fieldname;
}
}
$ Fields_with_lengths = array ('menu _ name' => 30 );
Foreach ($ fields_with_lenhths as $ fieldname => $ maxlength ){
If (strlen (trim (mysql_prep ($ _ POST [$ fieldname])> $ maxlength ){
$ Errors [] = $ fieldname ;}
}
If (empty ($ errors )){
$ Id = mysql_prep ($ _ GET ['subj']);
$ Menu_name = mysql_prep ($ _ POST ['menu _ name']);
$ Position = mysql_prep ($ _ POST ['position']);
$ Visible = mysql_prep ($ _ POST ['visable']);
$ Query = "UPDATE subjects SET
Menu_name = '{$ menu_name }',
Position = {$ position },
Visible = {$ visible}
WHERE id = {$ id }";
$ Result = mysql_query ($ query, $ connection );
If (mysql_affected_rows () = 1 ){
$ Message = "The topic has been successfully updated ";
} Else {
$ Message = "an error occurred while updating the topic! ";
$ Message. ="
". Mysql_error ();
}
} Else {
$ Message = "the error". count ($ errors). "is displayed in the form ";
}
}
?>
Reply to discussion (solution)
Try this way:
Foreach ($ required_fields as $ fieldname)
{
If (! Isset ($ _ POST [$ fieldname]) | empty ($ _ POST [$ fieldname]) | intval ($ _ POST [$ fieldname]) <= 0)
{
$ Errors [] = $ fieldname;
}
}
The judgment starting from if (isset ($ _ POST ['submit ']) {is also a false one!
Whether or not the code is established, subsequent code will be executed
(Empty ($ _ POST [$ fieldname]) & $ _ POST [$ fieldname]! = 0)
It means that $ _ POST [$ fieldname] is a value greater than 0. Is it your expectation?
Thank you very much for your answers. I am studying a tutorial, which is based on the tutorial step by step. The same code is different from the result in the example. I 'd like to know why.
$ Required_fields = array ('menu _ name', 'position', 'visable ');
Foreach ($ required_fields as $ fieldname ){
If (! Isset ($ _ POST [$ fieldname]) | (empty ($ _ POST [$ fieldname]) & $ _ POST [$ fieldname]! = 0 )){
$ Errors [] = $ fieldname;
}
}
Does this function mean that if $ _ POST [$ fieldname] is not set, or $ _ POST [$ fieldname] is null and 0, then an error array $ errors [] = $ fieldname is returned. Is that true?
However, why is it not detected in reality? All subsequent code is executed.
Since it is process control, your subsequent code should be placed in the else branch. Because you have determined that illegal data is passed in.
Besides, I made a mistake yesterday.
Empty ($ _ POST [$ fieldname]) & $ _ POST [$ fieldname]! = 0 indicates Yes
Empty ($ _ POST [$ fieldname]) $ _ POST [$ fieldname] is empty
& At the same time
$ _ POST [$ fieldname]! = 0 $ _ POST [$ fieldname] is not equal to 0
Then this expression will never be true!
Since you copied the code, check it first.
You won't copy the correct question, right?
If (! Isset ($ _ POST [$ fieldname]) | (empty ($ _ POST [$ fieldname]) | $ _ POST [$ fieldname] = 0 )){
......
I watched the video, and the code was written in step by step. I verified it first, but it was not correct in comparison with the video, I still cannot see any difference. Thank you for your reply. I will sort out the above code format for your convenience.
The problem is solved. Follow these two suggestions.
Set: if (! Isset ($ _ POST [$ fieldname]) | (empty ($ _ POST [$ fieldname]) & $ _ POST [$ fieldname]! = 0 )){
$ Errors [] = $ fieldname;
}
Changed to: if (! Isset ($ _ POST [$ fieldname]) | empty ($ _ POST [$ fieldname]) | $ _ POST [$ fieldname] = 0 ){
$ Errors [] = $ fieldname ;}
}
But the question is, why can the original code be successfully executed on the video?