Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Boolean variable always comes out true in my form

I'm trying to validate my form so that 2 subjects are not chosen at the same time, so a student can't take comoting twice. If they do i assign the boolean to true. If the boolean isn't true i execute my code, but the boolean is always true for some reason

?php 
//require("includes/config.php");
error_reporting(E_ERROR | E_WARNING | E_PARSE);

if (isset($_POST['name'])) { //checks if the name has posted value then inserts the data
        $name     = $_POST['name'];
        $subject1 = $_POST['subject1'];
        $grade1   = $_POST['grade1']; 
        $subject2 = $_POST['subject2'];
        $grade2   = $_POST['grade2'];
        $subject3 = $_POST['subject3'];
        $grade3   = $_POST['grade3'];
        $subject4 = $_POST['subject4'];
        $grade4   = $_POST['grade4'];
        $attendance = $_POST['attendance'];
        $gender     = $_POST['gender'];

        // <--------------Validation for form Post----------------------->




        $val = false;

        //validation make sure  2 of the same subjects haven't been chosen 
        if ($subject1 = $subject2 or  $subject3 or  $subject4) {

            $val = true;
          } 

          elseif ($subject2 = $subject1 or $subject3 or $subject4) {

                $val = true;

          }
            elseif ($subject3 = $subject1 or $subject2 or $subject4) {

                $val = true;
            }




            if ($val === false) {

          //inserting the data once validated


            $q        = "INSERT INTO paststudent (name,subject1,grade1,subject2,grade2,subject3,grade3,subject4,grade4,attendance,gender) VALUES (:name, :subject1,:grade1,:subject2,:grade2,:subject3,:grade3,:subject4,:grade4,:attendance,:gender);"; //sql statement add a colon intead of variables so pdo can prepare the statement and prevent any sql injections 
            $query    = $db ->prepare ($q);
            $results  = $query ->execute (array(  

                    ":name"     => $name,  //points to the variable 
                    ":subject1" => $subject1,
                    ":grade1"   => $grade1,
                    ":subject2" => $subject2,
                    ":grade2"   => $grade2,
                    ":subject3" => $subject3,
                    ":grade3"   => $grade3,
                    ":subject4" => $subject4,
                    ":grade4"   => $grade4,
                  ":attendance" => $attendanc,
                  ":gender"     => $gender
                  )); 

                        header("location:thanks.html");

                      } else {

                        echo $val;
                        echo "Invalid form details, make sure you haven't entered the same subject twice";
        }
      }



?> 

2 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

Use == or probably better === for the comparison in your if statements and you may have to test each against the subject.

 if ($subject1 === $subject2 ||  $subject1 === $subject3 || $subject1 ===   $subject4) {

    $val = true;

} // other elseif's
Kevin Korte
Kevin Korte
28,149 Points

I concur with Sean T. Unwin

Single equals assigns a value. Double and triple equals are comparative operators. You'll also have to test each statement after to or. So to put into words what Sean wrote, for example the first one needs to say is subject1 equal to subject2 or is subject1 equal to subject3 or is subject1 equal to subject4`