PHP checks if there are null values in the form and why it doesn't work

Source: Internet
Author: User
Php

if (Intval ($_get[' subj ')) = = 0) {
Redirect_to ("content.php");
}
if (Isset ($_post[' submit ')) {
$errors =array ();
$required _fields = Array (' menu_name ', ' position ', ' visible ');
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[' visible ');
$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 = "column has been successfully updated";
}else{
$message = "Column update failed! ";
$message. = "
" . Mysql_error ();
}
}else{
$message = "appears in form." Count ($errors). "At fault";
}
}

?>


Reply to discussion (solution)

Try this:
foreach ($required _fields as $fieldname)
{
if (!isset ($_post[$fieldname)) | | empty ($_post[$fieldname]) | | intval ($_post[$fieldname]) <=0)
{
$errors [] = $fieldname;
}
}

From if (Isset ($_post[' submit ')) {Start the judging form with a dummy!
Any subsequent code will be executed, whether or not it is established.

(Empty ($_post[$fieldname]) && $_post[$fieldname]!=0)
Meaning is $_post[$fieldname] is a value greater than 0, is it what you expect?

Thank you very much for the above two-bit answer. I am learning a tutorial, which is done step-after-step according to the tutorial. The same code, but differs from the results in the example. I wonder what the reason is.

$required _fields = Array (' menu_name ', ' position ', ' visible ');
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 array of errors is returned $errors[] = $fieldname, is that so?
But why is it not detected in practice? The following code is executed.

Since it is process control, your subsequent code will be placed in the Else branch. Because you're judging by the passing of illegal data.
Besides, yesterday I was wrong.
Empty ($_post[$fieldname]) && $_post[$fieldname]!=0
Empty ($_post[$fieldname]) $_post[$fieldname] Null
&& at the same time
$_post[$fieldname]!=0 $_post[$fieldname] not equal to 0
Then this expression will never be true!

Since you are the code on the transcription, please check it first.

You're not going to be correcting the problem, are you?

if (!isset ($_post[$fieldname)) | | (Empty ($_post[$fieldname]) | | $_post[$fieldname] ==0)) {
......

I look at the video, code is I step by step write down, first verify, not in and video on the right, over many times, or can not see where there is a different place. Thank you for your reply. I'll put the above code in the format so that you can read it.

The problem is solved. Follow the two-bit recommendations above.
Will: if (!isset ($_post[$fieldname]) | | (Empty ($_post[$fieldname]) && $_post[$fieldname]! = 0)) {
$errors [] = $fieldname;
}

Instead: if (!isset ($_post[$fieldname]) | | empty ($_post[$fieldname]) | | $_post[$fieldname] = = 0) {
$errors [] = $fieldname; }
}
But the question is, why watch the video, the original code can be executed successfully?

  • Related Article

    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.