Creating Dynamic Links with PHP/MySQL

和自甴很熟 提交于 2019-12-23 05:35:31

问题


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.


回答1:


put mysql_close after you use mysql_result, but once you get it working you might look into a more modern approach like PDO.




回答2:


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




回答3:


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'] ) ."'" ;


来源:https://stackoverflow.com/questions/6398984/creating-dynamic-links-with-php-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!