0

I have created a forum where you can ask questions to musicians. I have a form where you can select multiple checkboxes to what kind of musicians you want to talk to.

With the POST data I did the following:

$result = implode(",",$_POST['sub']);

And insert it into the database table which look like this:

-------------------------------------------
| id  | topicname | genre |    Subgenre   |
-------------------------------------------
|  1  |   music   | Metal | Heavy, Thrash |
-------------------------------------------

So what I want is only musicians that have signed up on the site with either heavy or Thrash can interact with this forum topic. I know how to check it normally if there was only 1 variable in the table but now there is two and I have no clue how to solve this problem.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Hazelcraft
  • 113
  • 8
  • use the `OR` operator. `SELECT * FROM Table WHERE col1 = 'value' OR col2 = 'value2'` – Takarii Jun 03 '16 at 09:57
  • But i dont think i can use this because another topic could have 3 or 4 different subgenre checked when they are submitted to the database. – Hazelcraft Jun 03 '16 at 10:00
  • Consider your table structure. It might be worth thinking about how you are storing the information in the database such that data duplication and assignment becomes simpler. – Takarii Jun 03 '16 at 10:04

1 Answers1

0

why don't you use 'like' function in mysql. It retrive the record match with the string found in the table column. Just like following code

SELECT * FROM table_name WHERE `genre` LIKE '%metal%' OR `Subgenre` LIKE '%metal%'

Hope it solve your issue... Good Luck..

  • How would using `LIKE` solve anything? The values in the table are exact, and this isnt a search type request (or, rather, shouldn't be). – Takarii Jun 03 '16 at 10:00
  • SELECT * FROM TableName WHERE Column_name IN (column1, column2, column3, column4) – kiran gadhvi Jun 03 '16 at 10:40
  • for more reference http://stackoverflow.com/questions/5295714/array-in-sql-query – kiran gadhvi Jun 03 '16 at 10:48
  • If you merge more than one record in a single row with implode of comma then how IN function find the perfect match to serve the data. For Instance if I have a data like "Heavy, Thrash" and another record have ("Thrash, Heavy") from this case IN function wont retrieve the record so I use LIKE function to match the record. Because I too face same problem for one of my application and It working fine buy using LIKE method. Hope you understand my scenario... –  Jun 03 '16 at 11:14
  • Like actually worked! I could use it into mysql_row to see if i got 1 that reminded og the variable! Thanks alot! Have a wonderfulll weekend – Hazelcraft Jun 03 '16 at 14:56