我对HTML、PHP、JavaScript等都很陌生。
我有一个将由数据库上传的表,其中包含姓名、id、电子邮件等字段。
我想显示一个包含表中所有人的页面,并带有一个详细信息页面的链接,该页面将显示该人的所有属性(表中的字段)。
我使用MySQL,我正在尝试构建一个管理界面,以便更好地管理用户。
您的链接可能是这样的person.php?id=1243
其中person.php是显示具有id = 1243
的人员信息的页面
您可以使用$id = $_GET['id']
检索id,然后在mysql查询中使用它:
SELECT * from Person WHERE id = $id; // Don't forget to handle SQL injection
你完了。
编辑:对于多个字段
链路person.php?id1=1243&id2=5678
检索值$id1 = $_GET['id1']
和$id2 = $_GET['id2']
MySQL:SELECT * from Person WHERE id IN($id1, $id2);
编辑2:对于一系列用户
链路person.php?startId=1&endId=5000
检索值$startId = $_GET['startId']
和$endId = $_GET['endId']
MySQL:SELECT * from Person WHERE id BETWEEN $startID AND $endId;
由@PierreGranger建议的备选方案
你的链接可能是这样的person.php?id[]=1243&id[]=5678
其中person.php是显示具有id = 1243
和id = 5678
的人员信息的页面
您可以使用$id = $_GET['id'][0]
检索id,然后在mysql查询中使用它:
if ( isset($_GET['id']) && is_array($_GET['id']) ) // Check if parameter is set
{
$ids = $_GET['id'] ;
foreach ( $ids as $k => $v )
if ( ! preg_match('#^[0-9]+$#',$v) ) unset($ids[$k]) ; // Remove if parameters are not identifiers
if ( sizeof($ids) > 0 ) // If there is still at least 1 id, do your job
{
$sql = ' SELECT * from Person WHERE id in (".implode('","',$ids).") ' ;
$rq = mysql_query($sql) or die('SQL error') ;
while ( $d = mysql_fetch_assoc($rq) )
{
// Do your SQL request and build your table
}
}
}
Wich将导致:
SELECT * from Person WHERE id in ("1234","5678") ;
你完了。
根据Akram的响应,您可以尝试这个。有两种情况:如果你只有4或5个特定的id,你可以这样做:page.php?id[]=1&id[]=2&id[]=9
如果你想达到一个范围(id从1到1000):page.php?from=1&to=1000
unset($sql) ;
if ( isset($_GET['id']) && is_array($_GET['id']) ) // Check if parameter is set
{
$ids = $_GET['id'] ;
foreach ( $ids as $k => $v )
if ( ! preg_match('#^[0-9]+$#',$v) ) unset($ids[$k]) ; // Remove if parameters are not identifiers
if ( sizeof($ids) > 0 ) // If there is still at least 1 id, do your job
{
$sql = ' SELECT * from Person WHERE id in ("'.implode('","',$ids).'") ' ;
}
}
elseif ( isset($_GET['from']) && isset($_GET['to']) )
{
if ( preg_match('#^[0-9]+$#',$_GET['from']) && preg_match('#^[0-9]+$#',$_GET['to']) )
{
$sql = ' SELECT * from Person WHERE id between "'.$_GET['from'].'" and "'.$_GET['to'].'"' ;
}
}
if ( isset($sql) )
{
$rq = mysql_query($sql) or die('SQL error') ;
while ( $d = mysql_fetch_assoc($rq) )
{
// Do your SQL request and build your table
}
}