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

时间:2014-03-23 11:01:49

标签: php html mysql database redirect

在我的网站上,我有一个网页,其中列出了所有帖子(home.php)和一个显示整个帖子的网页(post.php)。我使用postid作为网址变量,它在post.php上工作正常(这意味着如果我将网址更改为post.php?postid=1post.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>

显示表格

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

3 个答案:

答案 0 :(得分:1)

可能是我误解了你的问题,或者你的代码中有一个简单的错误:在链接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> 

这样的事情,我想

答案 1 :(得分:1)

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

答案 2 :(得分:0)

确定。试试这个 - 获取帖子的数字数组。

<?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++;
 }
  ?>