I’m creating an edit page that has a many to many relationship between the banners table and the frames table.
The problem I’m having is that I’m getting duplicates of the frames.
I hope I’ve explained everything clearly.
So for example I have
Frame 1
Frame 1
Frame 2
Frame 2
Frame 3
Instead of
Frame 1
Frame 2
Frame 3
This is my tables
banners table
id | title
1 | Home banner
frames table
id | title | description | image
1 | Frame 1 | Frame 1 | frame.jpg
2 | Frame 2 | Frame 2 | frame2.jpg
3 | Frame 3 | Frame 3 | frame3.jpg
4 | Frame 4 | Frame 4 | frame4.jpg
5 | Frame 5 | Frame 5 | frame5.jpg
banner_frame table
id | banner_id | frame_id
1 | 1 | 1
2 | 2 | 1
2 | 2 | 5
Here is my code
$query = "SELECT frames.id as frameId, frames.title as frameTitle, banners.id as banner_id ";
$query .= "FROM frames ";
$query .= "LEFT JOIN banner_frame ON banner_frame.frame_id = frames.id ";
$query .= "LEFT JOIN banners ON banners.id = banner_frame.banner_id";
$banner_frame = mysqli_query($conn, $query);
confirmQuery($banner_frame);
while($row = mysqli_fetch_assoc($banner_frame))
{
$frame_id = $row['frameId'];
$frame_title = $row['frameTitle'];
$checked = '';
if ($row['banner_id'] == $banner_id) {
$checked = 'checked';
}
echo "<div class='form-check'>";
echo "<input type='checkbox' name='frames[]' id='frame_checkbox' value='$frame_id' $checked> ";
echo "<label for='frame_checkbox'>";
echo $frame_title;
echo "</label>";
echo "</div>";
}