1

My parameters have different names (tags, tags2, tags3) but the same GET method or in other words the same column name (tags) in the MySQL database.

Here is the code:

$tags = $_GET['tags'];
$tags2 = $_GET['tags'];
$tags3 = $_GET['tags'];

$fetch = mysqli_query($conn, "SELECT * FROM users JOIN user_images ON users.username = user_images.username WHERE tags = '$tags' AND tags = '$tags2' AND tags = '$tags3' ORDER BY users.id DESC");

Basically I am trying to display all images with 3 different tags in the user_images tables.

Here is what my URL looks like, but it only displays one tag and not all 3:

https://www.example.com/app/user-tags.php?tags=aesthetic&tags2=food&tags3=dogs

The first parameter works, but not all of them. Can anyone help me where I went wrong?

I thought about doing something likes this, having tags = aesthetic,food,dogs in the query. But the problem is that I think the result would read that as, "Find all images with the tags of aestheticfooddogs" instead of tags with aesthetic, food, and dogs.

7
  • Change $tags2 = $_GET['tags']; to $tags2 = $_GET['tags2']; and do the same for tags3. Commented Feb 16, 2019 at 16:48
  • @Dave Ok I did it, same to $tags3, but this time nothing displays. I thought the $_GET holds the name of the column from the SQL Database, that's why I kept it the same. Commented Feb 16, 2019 at 16:52
  • $_GET is the array of parameters from the URL. The sample URL you used has 3 names and values, tags, tags2 and tags3. tags contains aesthetic, tags2 contains food and tags3 contains dogs. If you var_dump($_GET); before the code you posted what do you get? Commented Feb 16, 2019 at 16:55
  • Here are the results before the code, array(3) { ["tags"]=> string(9) "aesthetic" ["tags2"]=> string(4) "food" ["tags3"]=> string(4) "dogs" } . I also did it to the $_GET changes and got the same results. Commented Feb 16, 2019 at 17:00
  • My first comment stands and is corrected especially after you just verified what I said. $tags2 = $_GET['tags2']; will result in $tags2 containing food. You confusion is in your SQL query. How is your tags column in SQL defined? Commented Feb 16, 2019 at 17:02

1 Answer 1

2

It sounds like you want to look in the tags column in your table and find any matches for the values passed in the URL. If you want to find ANY of the tag values change the use of AND to OR instead.

$tags  = $_GET['tags'];
$tags2 = $_GET['tags2'];
$tags3 = $_GET['tags3'];

$fetch = mysqli_query($conn, "SELECT * FROM users
JOIN user_images ON users.username = user_images.username
WHERE tags LIKE '%" . $tags  . "%' OR
      tags LIKE '%" . $tags2 . "%' OR
      tags LIKE '%" . $tags3 . "%'
ORDER BY users.id DESC")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.