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

My sql and php query isn't working

Hi my sql query isn't working, it's meant to select a student that has taken the same subjects and got the same grades from the sql database. I get the data from a form where the user enters previous student's information, this gets posted and stored in variables,then the sql query is executed.

I also get the error : Parse error: syntax error, unexpected T_LOGICAL_AND in /home/u717042829/public_html/predicter.php on line 117 which is ``` if ($autumn != "") and ($winter = "") and ($spring = "") and ($summer = "") {

```  <?php 

    //posted form data getting stored into variables ready to use
    if (isset($_POST['name'])) {


        $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'];
        $autumn   = $_POST['autumn'];
        $winter   = $_POST['winter'];
        $spring   = $_POST['spring'];
        $summer   = $_POST['summer'];


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



       // error a boolean variable if an error pops up error is assigned to be false
        $error = true;

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

          }
        elseif ($subject3 == $subject1 or $subject3 == $subject2 or $subject3 == $subject4) {
             $error = false;
            }
            // checks if the data entered is an integer 

      elseif ( is_int( $attendance ) && $attendance >= 0 && $attendance <= 100 ) {
      $error = false; // If the above condition is met, $error is set to false
    }


// code exectured if no error detected 
  if ($error) {



      //selecting from the database
      // using pdo statements and preparing the statements to prevent sql injections 
      // stores the query value of db into query
      // try ifset 

    if ($autumn != "") and ($winter = "") and ($spring = "") and ($summer = "") {
       $query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1 AND subject2 = :subject2 AND grade2 = :grade2 AND subject3 = :subject3 AND grade3 = :grade3 AND subject4 = :subject4 AND grade4 = :grade4 AND autumn = :autumn");
        // binds the value to the query variable 
        $query->bindValue(':subject1', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade1', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject2', $subject2, PDO::PARAM_STR);
        $query->bindValue(':grade2', $grade2, PDO::PARAM_STR);
        $query->bindValue(':subject3', $subject3, PDO::PARAM_STR);
        $query->bindValue(':grade3', $grade3, PDO::PARAM_STR);
        $query->bindValue(':subject4', $subject4, PDO::PARAM_STR);
        $query->bindValue(':grade4', $grade4, PDO::PARAM_STR);
        $query->bindValue(':autumn', $autumn, PDO::PARAM_STR);
        // executes the query
        $query->execute();
        // the value of the query is stored to result
        $result = $query->fetch(PDO::FETCH_ASSOC);
    // fetch() instead of fetchAll just gets the first result



    }}


    //  ------ WINTER SELECT QUERY ------

      if ($winter != "") and ($winter = "") and ($spring = "") and ($summer = "") {
       $query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1 AND subject2 = :subject2 AND grade2 = :grade2 AND subject3 = :subject3 AND grade3 = :grade3 AND subject4 = :subject4 AND grade4 = :grade4 AND winter = :winter");
        // binds the value to the query variable 
        $query->bindValue(':subject1', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade1', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject2', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade2', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject3', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade3', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject4', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade4', $grade1, PDO::PARAM_STR);
        // executes the query
        $query->execute();
        // the value of the query is stored to result
        $result = $query->fetch(PDO::FETCH_ASSOC);
    // fetch() instead of fetchAll just gets the first result



    }}



    // --------- IF SPRING IS TRUE -------

    elseif ($autumn = "") and ($winter = "") and ($spring != "") and ($summer = "") {
       $query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1 AND subject2 = :subject2 AND grade2 = :grade2 AND subject3 = :subject3 AND grade3 = :grade3 AND subject4 = :subject4 AND grade4 = :grade4 AND spring = :spring");
        // binds the value to the query variable 
        $query->bindValue(':subject1', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade1', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject2', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade2', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject3', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade3', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject4', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade4', $grade1, PDO::PARAM_STR);
        // executes the query
        $query->execute();
        // the value of the query is stored to result
        $result = $query->fetch(PDO::FETCH_ASSOC);
    // fetch() instead of fetchAll just gets the first result



    }}

    // if statements to execute the relevant query 
    // ---------- IF SUMMER TERM IS TRUE ------ 
      elseif ($autumn = "") and ($winter = "") and ($spring = "") and ($summer != "") {
       $query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1 AND subject2 = :subject2 AND grade2 = :grade2 AND subject3 = :subject3 AND grade3 = :grade3 AND subject4 = :subject4 AND grade4 = :grade4 AND summer = :summer");
        // binds the value to the query variable 
        $query->bindValue(':subject1', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade1', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject2', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade2', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject3', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade3', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject4', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade4', $grade1, PDO::PARAM_STR);
        // executes the query
        $query->execute();
        // the value of the query is stored to result
        $result = $query->fetch(PDO::FETCH_ASSOC);
    // fetch() instead of fetchAll just gets the first result



    }}
    // if the query fails then the user is submitted to the error page which gives instructions on how to resolve the error
    else {
        header("location:error.php");
    }
    }?>

2 Answers

You are incorrectly using parenthesis on the line in question:

if ($autumn != "") and ($winter = "") and ($spring = "") and ($summer = "") {

}

an if statement should be wrapped in a set of parenthesis, even if there are multiple conditions:

if (a == 1 AND $sky == 'blue') {

}

also, I noticed that you are not doing a comparison on $winter, $spring, and $summer, you are assigning them the string value "". One equals sign is assignment, two is logical comparison, so I think you mean if $autumn is not "" and $winter, $spring, and $summer are equal to "", which would translate to the following:

if ($autumn != "" and $winter == "" and $spring == "" and $summer == "") {

}

Hi i changed the statement and still get the error Parse error: syntax error, unexpected T_LOGICAL_AND in /home/u717042829/public_html/predicter.php on line 117

``` if ($autumn != "") and ($winter == "") and ($spring == "") and ($summer == "") {

Furquan, the use of parenthesis is still incorrect:

It should be:

if ($autumn != "" and $winter == "" and $spring == "" and $summer == "") {

}

Thanks so much for your help Joe, sorry for being such a noob at programming.

My sql select query keeps on producing errors, not sure why.

  elseif ($autumn == "" and $winter == "" and $spring == "" and $summer != "") {
       $query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1 AND subject2 = :subject2 AND grade2 = :grade2 AND subject3 = :subject3 AND grade3 = :grade3 AND subject4 = :subject4 AND grade4 = :grade4 AND summer = :summer");
        // binds the value to the query variable 
        $query->bindValue(':subject1', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade1', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject2', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade2', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject3', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade3', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject4', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade4', $grade1, PDO::PARAM_STR);
        // executes the query
        $query->execute();
        // the value of the query is stored to result
        $result = $query->fetch(PDO::FETCH_ASSOC);
    // fetch() instead of fetchAll just gets the first result