使用 PHP/MySQL 创建动态链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6398984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Dynamic Links with PHP/MySQL
提问by Dan
i'm creating my first PHP/MySQL site and i'm having difficulty figuring out how to generate dynamic links and creating a new page for those links.
我正在创建我的第一个 PHP/MySQL 站点,但我很难弄清楚如何生成动态链接并为这些链接创建一个新页面。
My index page is pulling in certain details from my database as a preview, and when the visitor clicks on that item, i want them to be taken to a page which shows the full information from the database for that row.
我的索引页正在从我的数据库中提取某些详细信息作为预览,当访问者单击该项目时,我希望他们被带到一个页面,该页面显示该行的数据库中的完整信息。
The code on my index page for displaying the previews is below, any help on amending it to generate the link and page would be greatly appreciated.
我的索引页面上用于显示预览的代码如下,任何修改它以生成链接和页面的帮助将不胜感激。
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle"><?php echo $f2; ?></li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
</div>
<?php
$i++;
}
?>
I'm pretty sure what i'm asking is really simple, i just can't get my head around acheieving it.
我很确定我要问的是真的很简单,我就是无法理解它。
回答by ldg
put mysql_close afteryou use mysql_result, but once you get it working you might look into a more modern approach like PDO.
在使用 mysql_result之后放置 mysql_close ,但是一旦你让它工作,你可能会研究更现代的方法,比如 PDO。
回答by Dan
Thanks to you both for your answers, but i have managed to fix it (or work-around it) with this on my index page:
感谢你们的回答,但我已经设法在我的索引页面上修复它(或解决它):
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
do {?>
<div class = "hjl"><a href="paging.php?job_id=<?php echo $rsjobinfo['job_id'];?>">
<ul>
<li id = "jobtitle"><?php echo $rsjobinfo['job_title'];?></li><br />
<li id = "compname"><?php echo $rsjobinfo['company_name'];?></li>
</ul>
<ul>
<li id = "city"><?php echo $rsjobinfo['city'];?>,
<?php echo $rsjobinfo['country'];?></li>
</ul>
</a>
</div>
<?php } while ($rsjobinfo=mysql_fetch_assoc($result))?>
</div>
Followed by this on my content page:
在我的内容页面上紧随其后:
<?php
$job_id = $_GET['job_id'];
$query="SELECT * FROM $tbl_name WHERE job_id = $job_id";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
?>
Thanks for your help everyone.
谢谢大家的帮助。
Dan
担
回答by WooDzu
to your code add link (which I think you already have somewhere):
到您的代码添加链接(我认为您已经在某处):
//...................
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li>
//...................
<a href="<?php echo '?id='.$job_id; ?>">Read more...</a>
//...................
then your code must check for variable $_GET['id'], so put IF in the beginning of your code:
那么您的代码必须检查变量 $_GET['id'],因此将 IF 放在代码的开头:
$where = '';
if( isset($_GET['id']) && strlen($_GET['id']) > 0 ) {
$where = ' job_id = "'. mysql_real_escape_string( $_GET['id'] ) .'"' ;
}
<?php
$query="SELECT * FROM $tbl_name $where ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
<a href="<?php echo '?id='.$job_id; ?>">Read more...</a>
</div>
<?php
$i++;
}
?>
edit:Try changing the following line:
编辑:尝试更改以下行:
$where = " job_id = '". mysql_real_escape_string( $_GET['id'] ) ."'" ;