使用 PHP/MySQL 创建动态链接
我正在创建我的第一个 PHP/MySQL 网站,但我很难弄清楚如何生成动态链接并为这些链接创建新页面。
我的索引页面从数据库中提取某些详细信息作为预览,当访问者单击该项目时,我希望他们被带到一个页面,该页面显示数据库中该行的完整信息。
我的索引页面上用于显示预览的代码如下,任何有关修改它以生成链接和页面的帮助将不胜感激。
<?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 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢你们俩的回答,但我已经设法在我的索引页面上修复它(或解决它):
在我的内容页面上紧随其后:
感谢大家的帮助。
担
Thanks to you both for your answers, but i have managed to fix it (or work-around it) with this on my index page:
Followed by this on my content page:
Thanks for your help everyone.
Dan
在使用 mysql_result 之后放置 mysql_close,但是一旦你让它工作起来,你可能会考虑使用更现代的方法,比如 PDO。
put mysql_close after you use mysql_result, but once you get it working you might look into a more modern approach like PDO.
到您的代码添加链接(我认为您已经在某处):
那么您的代码必须检查变量 $_GET['id'],因此将 IF 放在代码的开头:
编辑: 尝试更改以下行:
to your code add link (which I think you already have somewhere):
then your code must check for variable $_GET['id'], so put IF in the beginning of your code:
edit: Try changing the following line: