Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to 'split' words entered in a text field on a form and query each word against the database
    primarykey
    data
    text
    <p>In index.php I have a form and in the form a text field: </p> <pre><code>&lt;form name="UserSearch" action="petSearch.php" method="GET"&gt; &lt;input type="text" name="petWish" value="" /&gt; &lt;/form&gt; </code></pre> <p>where the user enters what pets they want into the text box (seperated by commas) e.g dog, cat, fish, snake</p> <p>In petSearch.php, I use the following function to create a legal SQL string that can be used in a SQL statement:</p> <pre><code>&lt;?php $userPetWish = mysqli_real_escape_string($link, $_GET["petWish"]); ?&gt; </code></pre> <p>I then create the SQL statement and query the MySQL database:</p> <pre><code>&lt;?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"; } ?&gt; </code></pre> <p>So my question is:</p> <p>1) In order to search for each individual pet against the database, is it possible to <strong>'split'</strong> 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?</p> <p>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)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload