简体   繁体   English

根据用户输入从数据库中选择数据

[英]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. 我的问题是我几乎有30种食品类别,并不是所有区域都为30种食品类别中的每一个提供外卖,我试图仅显示该区域具有的食品类别,而不显示其他类别。 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. 到目前为止,我的查询显示了所有30个类别,无论该区域实际有多少个类别,并且当我将WHERE设置为restaurant_id以外的任何类别时,该类别都不再显示。

I have tried Inner joining 2/3 tables to make this query work, but no joy. 我尝试使用内部联接2/3表来使此查询有效,但没有任何乐趣。

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. 我试图从表中选择所有Pcode等于用户输入代码的表。 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. 每个餐厅交付的邮政编码都保存在Delivery_Pcode.Pcode中,$ searchq在页面顶部定义为输入的邮政编码$ _post值。

I have tried: WHERE Restaurant_name = $rest_name (also defined) WHERE Restaurant_name = $rest_name and Restaurant_id = $rest_id( also defined) 我尝试过:WHERE Restaurant_name = $ rest_name(也已定义)WHERE Restaurant_name = $ rest_name和Restaurant_id = $ rest_id(也已定义)

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 ."';";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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