如何从PHP中的URL获取多个参数? [英] How can I get multiple parameters from a URL in PHP?

查看:275
本文介绍了如何从PHP中的URL获取多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是index.php

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>';

出了点问题.我看到了这个网址"newrecords.php?mid =& id = 44" 我想看到像这样的"mid = 4& id = 4",并且在此之后

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

newrecords.php

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

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

获取它们是正确的方法吗?

Is it right way to get them?

推荐答案

您的网址格式不正确.您必须用&分隔每个键/值.如果&amp;是HTML格式,则更好.

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>');

所以结构是?key1=value1&key2=value2&key3=value3

这样,您可以使用$_GET['key1']$_GET['key2']$_GET['key3']来获取它们.

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

您使用isset的方式来获取它们,这很棘手,因为可能会设置它们但没有任何价值.您也可以使用empty().它实际上检查值是否为0或为空.因此,如果您不使用0作为记录ID,则可以更好地使用它.而且,如果它们是id,我也将对其进行转换(确保它们是整数)

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)){ ... }

这篇关于如何从PHP中的URL获取多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆