显示URL变量与数据库ID对应的每一行

I have a URL that is dynamically generated:

https://mywebsite.co.uk/report.php?taskid=25&rep=1&rep=2&rep=3

I can retrieve the variable for the task ID and rep fine:

if (isset($_GET['taskid'])) { $taskid = $_GET['taskid']; }
if (isset($_GET['rep'])) { $referenceID = $_GET['rep']; }

What I'm trying to do is create an SQL statement on the page that selects a row based on the rep number in the URL. For example:

SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND ID = $referenceID

However, when I echo the result of $referenceID it is always the last rep, so in this case 3. How do I select the rows from the database where ID = 1,2 & 3?

I then want to display each row, so it would be something like:

<table>
$result = mysqli_query($con,"SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND ID = $referenceID");
while($row = mysqli_fetch_array($result))
{
$ID = $row['ID'];
$NAME = $row['NAME'];
print "<tr><td>$ID</td><td>$NAME</td></tr>";
}
</table>

This query should return 3 rows in the table with the ID AND NAME in each row.

Your help would be appreciated.

First, you need to change your parameter name from rep to rep[] This will cause PHP $_GET['rep'] to return an array.

Then you need to implode the array to obtain a string with commas:

if (isset($_GET['rep'])) { 
    $referenceID = implode(',',$_GET['rep']); 
}

You have to change your SQL syntax to this:

SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND ID IN ($referenceID)

try this query

$result = mysqli_query($con,"SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND (ID <= $referenceID and ID>= 1)");

You should use array-style URL parameters, e.g.

https://mywebsite.co.uk/report.php?taskid=25&rep%5B%5D=1&rep%5B%5D=2&rep%5B%5D=3

Then $_GET['rep'] will be an array, and you can do:

$referenceIDs = implode(',', array_map(array($con, 'real_escape_string'), $_GET['rep']));
$sql = "SELECT TASK_ID, ID, NAME
        FROM mytable 
        WHERE TASK_ID = $taskid 
        AND ID IN ($referenceIDs)";