使用 PHP/MySQL 创建动态链接

发布于 2024-11-16 06:04:59 字数 974 浏览 1 评论 0原文

我正在创建我的第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耳钉梦 2024-11-23 06:04:59

感谢你们俩的回答,但我已经设法在我的索引页面上修复它(或解决它):

<?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>

在我的内容页面上紧随其后:

<?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 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

神经大条 2024-11-23 06:04:59

在使用 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.

故人的歌 2024-11-23 06:04:59

到您的代码添加链接(我认为您已经在某处):

//...................
<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>
//...................

那么您的代码必须检查变量 $_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++;
}
?>

编辑: 尝试更改以下行:

$where = " job_id = '". mysql_real_escape_string( $_GET['id'] ) ."'" ;

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:

$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'] ) ."'" ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文