简体   繁体   中英

Select data from DB based on user input

I am usually very good at retrieving data from a DB, but i have come to a stand still.

I have a search bar, which directs the user to another page to show the products they have searched for on the first page. My website allows users to search for takeaways in their area. On the second page i have a drop down list, this is used to search through the different food categories for that area.

My problem is i have almost 30 food categories, not all areas have takeaways joints for each of the 30 categories, i am trying to only show the food categories the area has and not show the others. My query so far shows all 30 categories, no matter the number of categories the area actually has, and when i set the WHERE to any other than restaurant_id the categories no longer show.

I have tried Inner joining 2/3 tables to make this query work, but no joy.

Delivery_Pcode Tbl
1   Del_code_id
2   Restaurant_ID
3   Pcode   
4   Del_Price


Rest_Category tbl
1   CategoryID
2   Cuisine_category
3   Category_img

Rest_Details Tel
1   Resturant_ID
2   Resturant_name
3   City_name
4   Cat_ID

Query

 $get_cats = "SELECT * FROM Rest_Category
                    INNER JOIN Rest_Details
                    ON Rest_Category.CategoryID = Rest_Details.Cat_ID
                    INNER JOIN Delivery_Pcode
                    ON Delivery_Pcode.Restaurant_ID = Rest_Details.Resturant_ID
                    WHERE Delivery_Pcode.Pcode= $searchq";

I am trying to select all from the tables where the Pcode is equal to the users input code. The postcodes each restaurant delivers to are saved in Delivery_Pcode.Pcode and $searchq is defined at the top of the page as the inputted postcode $_post value.

I have tried: WHERE Restaurant_name = $rest_name (also defined) WHERE Restaurant_name = $rest_name and Restaurant_id = $rest_id( also defined)

I don't know if i am thinking to deep into this, and there is a very simple method (i have a tendency to do that), or i need to delete the query and start again... and advise or guidance is much appreciated.

I know the query works, as it populates it is just the query which is causing me problems.

You can try adding quotes

"SELECT * FROM Rest_Category
                INNER JOIN Rest_Details
                ON Rest_Category.CategoryID = Rest_Details.Cat_ID
                INNER JOIN Delivery_Pcode
                ON Delivery_Pcode.Restaurant_ID = Rest_Details.Resturant_ID
                WHERE Delivery_Pcode.Pcode= '$searchq'";

or (before a proper input sanitize)

  "SELECT * FROM Rest_Category
                INNER JOIN Rest_Details
                ON Rest_Category.CategoryID = Rest_Details.Cat_ID
                INNER JOIN Delivery_Pcode
                ON Delivery_Pcode.Restaurant_ID = Rest_Details.Resturant_ID
                WHERE Delivery_Pcode.Pcode= '" . $searchq ."';";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM