URL变量-重定向到选定的帖子


URL Variable - redirecting to a chosen post

在我的网站上,我有一个网页,列出了所有的帖子(home.php)和一个显示整个帖子(post.php)。我使用postid作为URL变量,它在post.php上运行良好(这意味着如果我将URL更改为post.php?postid=1, post.php?postid=2等,则出现正确的帖子)。但是,我不知道如何更改home.php中的代码,以便它指向所选的帖子(现在它总是指向postid=1,我点击"阅读更多")。
home.php

<?php session_start(); 
php require_once('connection.php');
mysql_select_db($database_connection, $connection);
$query_post = "SELECT * FROM post";
$post = mysql_query($query_post, $connection) or die(mysql_error());
$row_post = mysql_fetch_assoc($post);
$totalRows_post = mysql_num_rows($post);
mysql_select_db($database_connection, $connection);
$query_joining = "SELECT * FROM image inner join post on post.postimage = image.imagename";
$joining = mysql_query($query_joining, $connection) or die(mysql_error());
$row_joining = mysql_fetch_assoc($joining);
$totalRows_joining = mysql_num_rows($joining);
...
?>

<ul class="listingposts">
  <?php
  while($row_joining = mysql_fetch_assoc($joining)) {       
  ?>
  <li>
    <a href="post.php?postid=<?php echo $row_post['postid']; ?>">
    <img src="images/<?php echo $row_joining['imagename']; ?>"></a>
    <h3><a href="post.php?postid=<?php echo $row_post['postid']; ?>">
    <?php echo $row_joining['title']; ?>
    </a></h3>
    <p><?php echo $row_joining['description']; ?></p>
    <a href="post.php?postid=<?php echo $row_post['postid']; ?>">Read more</a>
  </li>
  <?php
  }
  ?>   
</ul> 

post.php

<?php session_start(); 
php require_once('connection.php');
$colname_postviewing = "-1";
if (isset($_GET['postid'])) {
  $colname_postviewing = $_GET['postid'];
}
mysql_select_db($database_connection, $connection);
$query_postviewing = sprintf("SELECT * FROM post WHERE postid = %s", GetSQLValueString($colname_postviewing, "int"));
$postviewing = mysql_query($query_postviewing, $connection) or die(mysql_error());
$row_postviewing = mysql_fetch_assoc($postviewing);
$totalRows_postviewing = mysql_num_rows($postviewing);
mysql_select_db($database_connection, $connection);
$query_image = "SELECT * FROM image";
$image = mysql_query($query_image, $connection) or die(mysql_error());
$row_image = mysql_fetch_assoc($image);
$totalRows_image = mysql_num_rows($image);
...
?>
    <div>
     <img class="picture" src="images/<?php echo $row_postviewing['postimage']; ?>">
      <h1><?php echo $row_postviewing['title']; ?></h1>
        <?php if ($totalRows_postviewing == 0) {?>
        <h2>No post.</h2>
        <?php } // Show if recordset empty ?>
        <p><?php echo $row_postviewing['category']; ?></p>
    <p><?php echo $row_postviewing['text']; ?></p>
    </div>
<<p> 显示表/strong>
post, CREATE TABLE `post` (
  `postid` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `date` date NOT NULL,
  `category` varchar(100) NOT NULL,
  `text` longtext NOT NULL,
  `description` varchar(500) NOT NULL,
  `postimage` varchar(50) NOT NULL,
  PRIMARY KEY (`postid`),
  KEY `fk_post_image1_idx` (`postimage`),
  CONSTRAINT `fk_post_image1` FOREIGN KEY (`postimage`) REFERENCES `image` (`imagename`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
image, CREATE TABLE `image` (
  `imagename` varchar(50) NOT NULL,
  `shortlink` varchar(45) NOT NULL,
  `source` varchar(500) NOT NULL,
  PRIMARY KEY (`imagename`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

您根本不需要首先SELECT,因为INNER JOIN为您提供了所需的所有数据,并且您可以使用while($row_joining = mysql_fetch_assoc($joining))对其进行迭代,因此您应该使用$row_joining['postid']来创建您正在寻找的URL参数。

可能是我误解了你的问题,或者你只是在你的代码中有一个bug:在链接href属性中你使用$row_post变量,这在循环

时不会改变

如果存在确定的一对一链接,则可以简单地使用内连接:

<?php session_start(); 
php require_once('connection.php');
mysql_select_db($database_connection, $connection);
$query_post = "SELECT post.postid, image.imagename, image.title, post.description FROM image INNER JOIN post ON post.postimage = image.imagename";
$result_post = mysql_query($query_joining, $connection) or die(mysql_error());

?>

<ul class="listingposts">
  <?php
  while($row_post = mysql_fetch_assoc($result_post)) {       
  ?>
  <li>
    <a href="post.php?postid=<?php echo $row_post['postid']; ?>">
    <img src="images/<?php echo $row_post['imagename']; ?>"></a>
    <h3><a href="post.php?postid=<?php echo $row_post['postid']; ?>">
    <?php echo $row_post['title']; ?>
    </a></h3>
    <p><?php echo $row_post['description']; ?></p>
    <a href="post.php?postid=<?php echo $row_post['postid']; ?>">Read more</a>
  </li>
  <?php
  }
  ?>   
</ul> 

大概是这样吧

ok。试试这个-为文章获取数字数组。

<?php session_start(); 
php require_once('connection.php');
mysql_select_db($database_connection, $connection);
$query_post = "SELECT postID FROM post";
$post = mysql_query($query_post, $connection) or die(mysql_error());
$row_post = mysql_fetch_array($post,MYSQL_NUM);
$totalRows_post = mysql_num_rows($post);
mysql_select_db($database_connection, $connection);
$query_joining = "SELECT * FROM image inner join post on post.postimage = image.imagename";
$joining = mysql_query($query_joining, $connection) or die(mysql_error());
$row_joining = mysql_fetch_assoc($joining);
$totalRows_joining = mysql_num_rows($joining);
...
?>

<ul class="listingposts">
  <?php
$i=0;
  while($row_joining = mysql_fetch_assoc($joining)) {       
  ?>
  <li>
    <a href="post.php?postid=<?php echo $row_post[$i]; ?>">
    <img src="images/<?php echo $row_joining['imagename']; ?>"></a>
    <h3><a href="post.php?postid=<?php echo $row_post[$i]; ?>">
    <?php echo $row_joining['title']; ?>
    </a></h3>
    <p><?php echo $row_joining['description']; ?></p>
    <a href="post.php?postid=<?php echo $row_post[$i]; ?>">Read more</a>
  </li>
  <?php
 $i++;
 }
  ?>