根据用户输入从数据库中选择数据 [英] Select data from DB based on user input

查看:154
本文介绍了根据用户输入从数据库中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常非常擅长从数据库中检索数据,但是我已经停滞不前.

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.

我的问题是我几乎有30种食物类别,并非所有区域都具有30种食物类别的外卖店,我试图仅显示该区域具有的食物类别,而不显示其他食物类别.到目前为止,我的查询显示了所有30个类别,无论该区域实际有多少个类别,并且当我将WHERE设置为restaurant_id以外的任何类别时,该类别都不再显示.

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.

我尝试使用内联2/3表来使此查询正常工作,但没有任何乐趣.

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

查询

 $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";

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

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.

我尝试过: WHERE Restaurant_name = $ rest_name(也已定义) 在哪里,餐厅名称= $ rest_name和餐厅ID = $ rest_id(也已定义)

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.

推荐答案

您可以尝试添加引号

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

这篇关于根据用户输入从数据库中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆