简体   繁体   中英

mysql select query 1 of the multiple values in selected field

I had another form to upload the details of 1 product which have multiple values (10) but let's say we choose New Arrival, Black, White and upload to database. So right now my database will have the field name as 'category' which has the values New Arrival, Black, White.

Now I want to do a search function but whenever I try running it, it just won't display the result. So i made 2 records where:

1st record with field category and value 'New Arrival' 2nd record with field category and value 'New Arrival, Black, White'

When I try running my code again, it did return the result of the 1st record and I tried duplicating the same records for a few rows and it turns out that it can only return the result where the category field has only 1 value.

Below is just a brief part of my code:

My add record form input for the category field is:

add_new.html

<select name="category[]" size="10" multiple="multiple">
<options>New Arrival</options>
<options>Black</options>
<options>White</options>
</select>

add_process.php

$category = implode(", ", $_POST['category']);
$strSQL = "INSERT INTO products_list (category) VALUES ('$category')";

search_form.html

<input type="text" name="search_text" />
<input type="submit" name="submit" />

search_process.php

$category = mysql_real_escape_string($_POST['product_category']);
$select = mysql_query("select image1, image2, image3, image4 from products_list WHERE category IN ('new arrival') ORDER BY id ASC");

while($row=mysql_fetch_array($select)) {

    echo $row['image1'];
    echo $row['image2'];
    echo $row['image3'];
    echo $row['image4'];

}

To repeat my question, how to get the result of the rows that contains the (desired value) in that category field?

Next question is, the category value is stored as 'New Arrival' only in the database, how do I get the return result if I were to type 'arrival' only instead of the full name? Currently it won't return any result too if I were to type only 'arrival'.

Hope you guys understand what I'm trying to say. Thanks in advance guys.

塞尔说:

$select = mysql_query("select image1, image2, image3, image4 from products_list WHERE category like '%new arrival%' ORDER BY id ASC");

For easier reference, i put the explanation here.

$catsearch = $_POST["category"]; 
$keywords = explode(' ', $catsearch);  //if keywords entered is "white black new", will convert to array of "white","black","new". The delimiter here is the space.
$keywordsBits = array(); 
foreach ($keywords as $keyword) { 
          $keyword = trim($keyword); 
          if (!empty($keyword)) { 
                $keywordsBits[] = "category LIKE '%$keyword%'"; 
          } 
} 

$result = mysql_query("SELECT * FROM products_list WHERE ".implode(' OR ', $keywordBits));

This will result a query like

SELECT * FROM products_list WHERE category LIKE '%white%' OR category LIKE '%black%' OR category LIKE '%new%'

If you want to separate the keywords by ",", you could use

$keywords = explode(',', $catsearch);  //if keywords entered is "white,black,new arrival", will convert to array of "white","black","new arrival". The delimiter here is the comma.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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