How can I get multiple parameters from a URL in PHP?

旧城冷巷雨未停 提交于 2019-12-12 05:01:25

问题


I have 2 variables to send records.php, '$id' and '$row[0]". After it, I want to use them in newrecords.php with $_Get method.

Here 's index.php

 echo '<td align="center">'.$row[0].'-)&nbsp;<a href="newrecords.php?mid=&id=' . $id . $row[0] . '"$ >'.  $row[3] . "&nbsp;" . $row[4] .'</a></td>';

There's something going wrong.I see this url "newrecords.php?mid=&id=44" i want to see like this "mid=4&id=4" and after this

newrecords.php

if (isset($_GET['mid']))
    {
        $id = $_GET['mid'];
}

if (isset($_GET['id']))
        {
            $nid = $_GET['id'];
    }

Is it right way to get them?


回答1:


You URL is malformed. You have to separate each key/value with a &. And better use &amp; if it's in HTML.

echo('<td align="center">'.$row[0].'-)&nbsp;<a href="newrecords.php?id='.
      $id.'&amp;mid='.$row[0].'"$ >'.$row[3]."&nbsp;".$row[4].'</a></td>');

So the structure is ?key1=value1&key2=value2&key3=value3

That way you can get them with $_GET['key1'] and $_GET['key2'] and $_GET['key3'].

The way you get them, you are using isset, that's tricky because they might be set but have no value. You can also use empty() for that. It actually checks whether a value is not 0 or empty. So if you don't use 0 as a record id you can better use that. And I also if they are id's I would cast them (making sure they are integers)

$id = (int)$_GET['id'];
if (!empty($id)){ ... }



回答2:


your url is not correct try this

echo '<td align="center">'.$row[0].'-)&nbsp;<a href="newrecords.php?mid='.$id.'&id='.$row[0].'" >'.  $row[3] . '&nbsp;' . $row[4] .'</a></td>';



回答3:


What you are doing? i think it is wrong. It will show url like "newrecords.php?mid=&id=44" this instead of your code you should try like this.

echo '<td align="center">'.$row[0].'-)&nbsp;<a href="newrecords.php?mid='.$id.'&id='.$row[0].'" >'.  $row[3] . '&nbsp;' . $row[4] .'</a></td>';


来源:https://stackoverflow.com/questions/25177673/how-can-i-get-multiple-parameters-from-a-url-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!