Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8410659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:14:09+00:00 2026-06-10T00:14:09+00:00

I am doing a tutorial on PHP. I am in the update section of

  • 0

I am doing a tutorial on PHP. I am in the update section of the CRUD lesson and am working on a validation script. I have followed the code exactly… I think but still not working correctly. Here is the problem: When I click submit to try to test the validation with the menu name field empty it goes through without an error… and updates the nav with an empty list item? I have looked over the code and all seems to be correct but I am very new to this so problems are easily overlooked.

Here is the code:

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;
        }
    }

    if(empty($errors)) {
        //Perform Update
        $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){
            // success
            $message = "Subject was sucessfully updated"; 
        }else{
            $message = "The subject update failed.";
            $message .= "<br />" . mysql_error();
        }
    }else{
        //error occured
        $message = "There were " . count($errors) . " errors in the form.";
    }
} // end if(isset($_POST['submit']) )
?>

And the code from the body:

<h2>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h2>
<?php 
    if(!empty($message)){
        echo "<p class=\"message\">" . $message . "</p>";
    }
?>
<?php
    if(!empty($errors)){
        echo "<p class=\"errors\">";
        echo "Please review the folowing fields: <br />";
        foreach($errors as $error){
            echo "-" . $error . "</p>";
        }
    }
?>

Now when I change (empty($_POST[$fieldname]) to (empty($_POST[‘menu_name’]) it spits out errors but they are for the position and visible fields which happen to be correct…? Is there a character left out that I just cannot see. I have done mostly copy and paste to prevent those stupid kind of mistakes.. Note: that the validation for position and visible do work… I think… since you cannot leave those fields blank anyway how would I know…

Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:14:11+00:00Added an answer on June 10, 2026 at 12:14 am

    The error is right here:

    $_POST[$fieldname] != 0
    

    An empty string = 0 because 0 is false. So in your if statement, you say if it is empty (which it is) AND NOT equal to 0 (which it does).

    What you can do is

    1. Drop the !=0 statement completely as empty will do a grand job checking that.
    2. Use the correct operator: !==”0″, if you want to accept “0” as an option.

    When you use 3 equals or !== it validates against the type. 0===0, and 0==false, “0”==0, “0”==false, and false===false, BUT 0!==false, 0!==”0″ and “0”!==false.
    I hope that makes sense.

    EDIT

    Lets run this down into the ground a little further with a series of tests.

    $a='';
    $b="0";
    if(empty($a))
        echo'$a is empty<br/>';
    if(empty($b))
        echo'$b is empty<br/>';
    if($a!=0)
        echo'$a does not equal 0/false<br/>';
    if($b!=0)
        echo'$b does not equal 0/false<br/>';
    if($a!==0)
        echo'$a does not equal integer 0<br/>';
    if($b!==0)
        echo'$b does not equal integer 0<br/>';
    if($a!=="0")
        echo'$a does not equal string 0<br/>';
    if($b!=="0")
        echo'$b does not equal string 0<br/>';
    

    The output above would be

    $a is empty
    $b is empty
    $a does not equal integer 0
    $b does not equal integer 0
    $a does not equal string 0
    

    Conclusions: $a==$b, $a!==$b. $a AND $b are == to 0 and == to false, neither of them are integers, both are strings, and an empty string is not the same as a string containing the 0 character.

    Change $_POST[$fieldname]!=0 to $_POST[$fieldname]!=="0"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in a tutorial I am doing, the code below is in index.php file. Does
I am doing a PHP tutorial that uses the code below to insert a
I am basically doing a tutorial but my program seems to have errors that
I am doing the following tutorial http://msdn.microsoft.com/en-us/library/ms731835%28v=vs.100%29.aspx and the program is working perfectly fine.
I'm currently doing facebook integration and have gone through this tutorial . So far,
Every ajax chat tutorial ends the same. You run a getChatMsg.php or some script
So following http://www.techotopia.com/index.php/Creating_an_Interactive_iOS_4_iPhone_App , I'm doing the tutorial in a tabbed application template which
I'm doing this piping php script that fetch the arriving emails and the parse
Is there a way to run the following script via PHP http://www.magentoadvisor.com/magento-backup/tutorial-magento-backup-scripts-part-2/ I have
I have found this tutorial online http://net.tutsplus.com/tutorials/php/creating-a-php5-framework-part-1/ I have created myself a simple sort

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.