如何“分割”在表单上的文本字段中输入的单词,并根据数据库查询每个单词

如何“分割”在表单上的文本字段中输入的单词,并根据数据库查询每个单词

问题描述:

In index.php I have a form and in the form a text field:

<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) e.g 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:

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

I then create the SQL statement and query the MySQL database:

<?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? 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. 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)

在index.php中我有一个表单,其形式为文本字段: p>

 &lt; form name =“UserSearch”action =“petSearch.php”method =“GET”&gt; 
&lt; input type =“text”name =“petWish”value =“”/&gt;  
&lt; / form&gt; 
  code>  pre> 
 
 

用户在文本框中输入他们想要的宠物(用逗号分隔),例如狗,猫,鱼,蛇 p>

在petSearch.php中,我使用以下函数创建可在SQL语句中使用的合法SQL字符串: p>

 &lt;  ?php 
 $ userPetWish = mysqli_real_escape_string($ link,$ _GET [“petWish”]); 
?&gt; 
  code>  pre> 
 
 

然后我创建了SQL语句, 查询MySQL数据库: p>

 &lt;?php 
 $ query =“SELECT petType,name,age FROM pets WHERE petType LIKE'%”。$ userPetWish。“%'  “; 
 $ result = mysqli_query($ link,$ query); 
 
 if if($ result)
 {
 while($ row = mysqli_fetch_array($ result))
 {
 echo”from the  PE 你进入我们有“。  $ row [petType]。  “,”。  $ row [name]。  “,”。  $ row [age]; 
} 
} 
 else 
 {
 echo“抱歉,我们没有您选择的任何宠物”; 
} 
?>&gt; 
  code>   pre> 
 
 

所以我的问题是: p>

1)为了针对数据库搜索每个宠物,是否可以'拆分 ' strong>用户通过逗号输入的宠物,然后使用“LIKE”功能查询数据库中的Pet表? 如果是这样,你能告诉我如何构建它吗? p>

2)如果在对数据库进行搜索之后,存在狗,猫和鱼,但没有蛇。 如何让它显示这一点,以显示他们对三只宠物的宠物类型,名称和年龄,而不是蛇? (我不确定我在while循环中构造它的方式是否正确) p> div>

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;

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 )

1. You can try using a MATCH AGAINST query.

$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. (read more here)

2. Another way to accomplish this would be with the LIKE search that you are familiar with

$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(....