简体   繁体   English

PHP使用GET搜索带有类别的mysql数据库

[英]PHP search mysql database with categories using GET

As a new project I recently tried creating a webshop just to see how it works. 作为一个新项目,我最近尝试创建一个网上商店,以了解其工作原理。 I already display all the new items which are on sale in the main div using php and a select tag. 我已经使用php和select标签显示了所有主要在div出售的新商品。 And I'm also able to only display items from a certain category when the user clicks on a category in the sidebar. 而且,当用户单击侧边栏中的类别时,我也只能显示特定类别的项目。 When the user clicks the sidebar the page is reloaded but to the url it adds ?category=car or some other category. 当用户单击侧栏时,页面会重新加载,但会在URL中添加?category = car或其他类别。 After that I use $_GET[' 之后,我使用$ _GET ['

if(isset($_GET['category'])){
     $testsearch = htmlspecialchars($_GET['category']);
     $sqlselect = "SELECT * FROM items_sale INNER JOIN item_list ON items_sale.item_id = item_list.item_id  INNER JOIN user_list ON items_sale.user_id = user_list.user_id  INNER JOIN categories ON items_sale.categorie_id = categories.category_id where category = '$testsearch'";
}else{
    $sqlselect = "SELECT * FROM items_sale INNER JOIN item_list ON items_sale.item_id = item_list.item_id  INNER JOIN user_list ON items_sale.user_id = user_list.user_id  INNER JOIN categories ON items_sale.categorie_id = categories.category_id";
}

What I can't seem to get to work is to also add something like a search bar where the user can search for the seller's name or the name of a item while staying within the selected category. 我似乎无法正常工作的是,还添加了诸如搜索栏之类的功能,用户可以在其中停留在选定类别内的同时搜索卖方的名称或商品名称。

Is there a simple way to do this? 有没有简单的方法可以做到这一点? Because I can only think of a bunch of If statements within each other to see what option is selected. 因为我只能想到彼此之间的一堆If语句,以查看选择了哪个选项。 Also if there are better options for a simple search system like this or more secure thats fine to of course. 同样,如果像这样的简单搜索系统有更好的选择,或更安全的选择当然也可以。 This is just how I've been doing it so far 到目前为止,这就是我一直在做的事情

Here is a screenshot of my current table btw 这是我当前表的屏幕快照 这是我目前的桌子

firstly, i recommand you to don't put chars in an url, but id. 首先,我建议您不要在URL中添加字符,而要输入ID。 It's safer, simplier and no error with url encoding or else. 它更安全,更简单,并且使用url编码也不会出错。 And you just have to cast the id you get. 您只需要强制转换您获得的ID。 It will be quicker too to search in mysql database with an id than a string :) 在id大于字符串的mysql数据库中搜索也会更快:)

For your question, you can put your category name (or id :) ) in a form with an input hidden. 对于您的问题,您可以将类别名称(或id :))放入隐藏了输入的表单中。

Example : 范例:

<form>
    <input type="text" id="seller_name">
    <input type="hidden" id="category" value="<?php echo (int)$_GET['category']?>">
</form>

On your server side, when you receive the request, you check category and seller's name in your sql request (with 2 if for example...) 在服务器端,当您收到请求时,您在sql请求中检查类别和卖方的名称(例如,如果有2,则为...)

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

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