dongxuxian6930 2013-12-06 09:58
浏览 15
已采纳

显示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.

  • 写回答

3条回答 默认 最新

  • dsj1961061 2013-12-06 10:24
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大