简体   繁体   English

如何从PHP中的URL获取多个参数?

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

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. 我有2个变量来发送records.php,“ $ id”和“ $ row [0]”。之后,我想通过$ _Get方法在newrecords.php中使用它们。

Here 's index.php 这是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?mid =&id = 44”,我希望看到这样的“ mid = 4&id = 4”,之后

newrecords.php newrecords.php

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

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

Is it right way to get them? 获得它们的正确方法吗?

您的网址不正确,请尝试此

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

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. 它将显示类似“ newrecords.php?mid =&id = 44”的网址,而不是您应该尝试的代码。

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

You URL is malformed. 您的网址格式不正确。 You have to separate each key/value with a & . 您必须使用&分隔每个键/值。 And better use &amp; 更好地使用&amp; if it's in HTML. 如果是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 所以结构是?key1=value1&key2=value2&key3=value3

That way you can get them with $_GET['key1'] and $_GET['key2'] and $_GET['key3'] . 这样,您可以使用$_GET['key1']$_GET['key2']$_GET['key3']来获取它们。

The way you get them, you are using isset, that's tricky because they might be set but have no value. 获取它们的方式是使用isset,这很棘手,因为可能会设置它们但没有价值。 You can also use empty() for that. 您也可以为此使用empty() It actually checks whether a value is not 0 or empty. 它实际上检查值是否为0或为空。 So if you don't use 0 as a record id you can better use that. 因此,如果您不使用0作为记录ID,则可以更好地使用它。 And I also if they are id's I would cast them (making sure they are integers) 而且,如果它们是id,我也将其强制转换(确保它们是整数)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM