I have the following 3 tables in my database.
Table: Images
Columns: Image_ID, Title, Description, Author, Date, Path, Rating
Table: Tags
Columns: Tag_ID, Title
Table: TagsConnection
Columns: Image_ID, Tag_ID
In order to get all the images that correspond to a certain tag, I execute this code.
<?php
$tagName = $_SERVER['QUERY_STRING'];
$realTagName = substr($tagName, 1);
$realestTagName = str_replace('%20', ' ', $realTagName);
$sql = "SELECT * FROM tags WHERE tag='$realestTagName'";
$result = mysqli_query($conn, $sql);
$getResult = mysqli_fetch_assoc($result);
$tagId = $getResult['id'];
$sql2 = "SELECT * FROM tagsconnection WHERE tagid='$tagId'";
$result2 = mysqli_query($conn, $sql2);
while ($row = mysqli_fetch_assoc($result2)) {
$imageId = $row['imageid'];
$sql3 = "SELECT * FROM image WHERE id='$imageId'";
$result3 = mysqli_query($conn, $sql3);
$getResult3 = mysqli_fetch_assoc($result3);
echo '<div class="imageContainer">
<h1>'.$getResult3['name'].'</h1>
<a href="imageInfo.php?='.$getResult3["path"].'">
<img class="uploadedImg" src="uploads/'.$getResult3["path"] .'" alt="Random image" />
</a>
</div>';
}
?>
I have this bad feeling that I'm not doing it the way it should be done, so I decided to ask around here and get a few tips and pointers if possible.