简体   繁体   English

如何“拆分”在表单的文本字段中输入的单词并针对数据库查询每个单词

[英]how to 'split' words entered in a text field on a form and query each word against the database

In index.php I have a form and in the form a text field: 在index.php中,我有一个表单和一个文本字段:

<form name="UserSearch" action="petSearch.php" method="GET">
     <input type="text" name="petWish" value="" />
</form>

where the user enters what pets they want into the text box (seperated by commas) eg dog, cat, fish, snake 用户在文本框中输入所需的宠物(用逗号分隔),例如狗,猫,鱼,蛇

In petSearch.php, I use the following function to create a legal SQL string that can be used in a SQL statement: 在petSearch.php中,我使用以下函数创建可以在SQL语句中使用的合法SQL字符串:

<?php    
    $userPetWish = mysqli_real_escape_string($link, $_GET["petWish"]);
?>

I then create the SQL statement and query the MySQL database: 然后,我创建SQL语句并查询MySQL数据库:

<?php
    $query="SELECT petType, name, age FROM pets WHERE petType LIKE '%".$userPetWish."%'";
    $result = mysqli_query($link, $query);

      if($result)
      {
          while($row = mysqli_fetch_array($result))
          {
             echo "From the pets you entered we have" . $row[petType] . "," . $row[name] . "," . $row[age];
          }
      }
      else
      {
         echo "Sorry we do not have any of your chosen pets in store";
      }
?>

So my question is: 所以我的问题是:

1) In order to search for each individual pet against the database, is it possible to 'split' the pets which the user entered by commas and then query the Pet table in the database using the "LIKE" function? 1)为了针对数据库搜索每个宠物,是否可以用逗号”“拆分”用户输入的宠物,然后使用“ LIKE”功能查询数据库中的宠物表? If so, can you tell me how to structure this? 如果是这样,您能告诉我如何组织吗?

2) If after doing the search against the database there is a dog, cat and fish in store, but no snake. 2)如果在对数据库进行搜索之后,存储的是狗,猫和鱼,而没有蛇。 How do i get it to display this, to show the petType, name and age they have of the three pets, but not the snake? 我如何显示它,显示三种宠物的宠物类型,名称和年龄,而不是蛇? (I'm not sure if the way I structured this in the while loop is correct) (我不确定在while循环中构造此结构的方式是否正确)

You can use 您可以使用

explode( ',', $userPetWish )

to split the words up. 分开的话。

Also a note I would use 另外我会用的笔记

echo 'From the pets you entered we have' . $row['petType'] . ',' . $row['name'] .
    ',' . $row['age'];

Instead of no quotes within the $row; 而不是在$ row中没有引号;

Also your better of stripping the slashes strip_slashes ( $userPetWish ) if your using a LIKE otherwise your going to get slashes in the query and might break it ( may be wrong, just what ive experienced ) 另外,如果您使用LIKE,则最好去除斜杠strip_slashes($ userPetWish),否则将在查询中获得斜杠并可能会破坏斜杠(可能是错误的,正是我所经历的)

1. You can try using a MATCH AGAINST query. 1.您可以尝试使用MATCH AGAINST查询。

$petArray = explode(',', $userPetWish);
$query = "SELECT petType, name, age FROM pets MATCH (petType) AGAINST ('".implode("','", $petArray)."')";
$result = mysqli_query(....

The result query should look like: 结果查询应如下所示:

SELECT petType, name, age FROM pets MATCH (petType) AGAINST ('cat', 'dog', 'fish')

Please note that for this to work you should create a FULLTEXT index on your petType column. 请注意,要执行此操作,您应该在petType列上创建FULLTEXT索引。 ( read more here ) 在这里阅读更多

2. Another way to accomplish this would be with the LIKE search that you are familiar with 2.完成此操作的另一种方法是使用您熟悉的LIKE搜索

$petArray = explode(',', $userPetWish);

$query = "SELECT petType, name, age FROM pets WHERE ";
$queryTemp = "";

foreach ($petArray as $pet) {
    if($queryTemp!=''){
        $queryTemp += ' OR '; 
    }
    $queryTemp += " petType LIKE '%".$pet."%' ";
}

$query += $queryTemp;
$result = mysqli_query(....

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

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