Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP returns SQL error when appending "AND" expression after "ORDER BY" expression in SQL query

In documentation I can see ORDER BY expression is usually put at the end of the query, however, in my case, I want to order products by price first and then choose brands, as a result, I would add " AND product_brand IN" after ORDER BY product_price. Then PHP throw an error, i’m pretty sure it is because I put AND express after ORDER BY expression, how can I get over this?

if(isset($_POST['action'])) {
    $keyword =  $_POST['keyword'];
    $output = "";
    $query = "SELECT * FROM $keyword WHERE product_rating = '4.5'";
    if (isset($_POST['general'])) {
        $general_filter = implode("','", $_POST['general']);
        switch ($general_filter) {
            case 'all':
                $query .= "";
                break;
            case 'low_to_high':
                $query .= "ORDER BY product_new_price ASC";
                break;
            case 'high_to_low':
                $query .= "ORDER BY product_new_price DESC";
                break;
            case 'name_ascending':
                $query .= "ORDER BY product_title ASC";
                break;
            case 'name_descending':
                $query .= "ORDER BY product_title DESC";
                break;
            case 'newest':
                $query .= "ORDER BY product_date DESC";
                    break;
            case 'top_seller':
                $query .= "ORDER BY product_sold ASC";
                break;
            case 'available':
                $query .= "AND product_status = '1'";
                break;
        }
    }
    if (isset($_POST['brand'])) {
        $brand_filter = implode(",", $_POST['brand']);
        if ($brand_filter == 'all') {
            $query .= "";
        }
        else {
            $query .= " AND product_brand IN ('$brand_filter')";
        }
    }

I search for hours on internet and still stuck.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

SQL has a specific order of commands. It is not allowed to have the orderBy before a WHERE clause.
You can solve this by moving the second if-statement up:

    $query = "SELECT * FROM $keyword WHERE product_rating = '4.5'";
        if (isset($_POST['brand'])) {
                $brand_filter = implode(",", $_POST['brand']);
                if ($brand_filter == 'all') {
                    $query .= "";
                }
                else {
                    $query .= " AND product_brand IN ('$brand_filter')";
                }
        }
        if (isset($_POST['general'])) {
            $general_filter = implode("','", $_POST['general']);
            switch ($general_filter) {
                case 'all':
                    $query .= "";
                    break;
                case 'low_to_high':
                    $query .= "ORDER BY product_new_price ASC";
                    break;
                case 'high_to_low':
                    $query .= "ORDER BY product_new_price DESC";
                    break;
                case 'name_ascending':
                    $query .= "ORDER BY product_title ASC";
                    break;
                case 'name_descending':
                    $query .= "ORDER BY product_title DESC";
                    break;
                case 'newest':
                    $query .= "ORDER BY product_date DESC";
                        break;
                case 'top_seller':
                    $query .= "ORDER BY product_sold ASC";
                    break;
                case 'available':
                    $query .= "AND product_status = '1'";
                    break;
            }
        }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading