Php URL变量-重定向到所选帖子
在我的网站上,我有一个网页列出了所有的帖子(Php URL变量-重定向到所选帖子,php,html,mysql,database,redirect,Php,Html,Mysql,Database,Redirect,在我的网站上,我有一个网页列出了所有的帖子(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()
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>
<?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
可能是我误解了您的问题,或者您的代码中有一个bug:在links href属性中,您使用$row\u post变量,该变量在while循环中没有更改 如果有确定的一对一链接,您可以简单地使用内部连接:
<?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>
-
像这样的,我想没问题。尝试此操作-获取帖子的数字数组
<?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++;
}
?>
-
您根本不需要首先选择
因为内部连接为您提供了所需的所有数据,而($row\u joining=mysql\u fetch\u assoc($joining))
您应该使用$row\u joining['postid']
来创建您要查找的URL参数。对,我一定是忽略了这一点。我有$row\u连接
循环图像,但没有postid
。但是如果我已经有一个循环,我该如何解决这个问题呢?你能帮我吗?那取决于你想要什么。正如您的代码所示,您需要为每个帖子获取所有帖子和一组图像,对吗?一篇帖子=一个图像和一个标题:)图像(以及关于它们的一些其他信息)存储在my db中的一个单独的实体中,而帖子有自己的实体。是否需要查看show create table
s?添加到问题能否显示post.phpsure,我马上编辑我的问题:)确定。我已经发布了答案,也许这会有所帮助!你可以用这样的逻辑。不管怎样编辑我的问题:)看一看,也许它会澄清情况;)好啊我使用mysql_fetch_数组来获取帖子。看到唯一的问题是如何在另一个循环中循环帖子。也许我的答案可以帮助我得到通知:未定义的偏移量:1 in…post.php
您是否使用mysql\u fetch\u数组并在数据库中检查帖子中的值