我正在尝试在网址中传递博客帖子的ID。它在adminpanel(阅读blogpost)中工作,现在我想在我的首页上做同样的事情,所以它说在每个博客帖子下阅读更多,但是当我使用相同的代码时,它不会传递id。
我的代码无效:
$db_connection = mysql_connect('localhost', 'root', '')or die("cannot connect");
mysql_select_db('database',$conn);
// mysql query
$sql_query="SELECT * FROM blog_posts ORDER BY id DESC";
// Create the ps_pagination object here
$pager = new ps_pagination($db_connection,$sql_query,5,5);
echo $pager->renderFullNav();
//The paginate() function returns a mysql result set
$rs = $pager->paginate();
while($rows = mysql_fetch_assoc($rs)) {
// table to display results here // modify here
echo '<h1>'.$rows["title"].'</p></h1>';
echo '<p> '.$rows["post"].'</p>';
echo '<p class="klein"><span>Door</span>: '.$rows["first_name"].' ';
echo '<p class="klein">Geplaatst op</span>: '.$rows["date_posted"].'</p>';
echo "<td><a class='link' href=\"/admin/bekijk.php?id=$row->id\">Lees meer</a></td>";
echo "<BR>";
}
// close mysql connection here
mysql_close();
我正在使用的代码:
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
echo "<h1><td><a class='link' href=\"bekijk.php?id=$row->id\">$row->title</a></td></h1>";
if($row->id == 0)
{ //home page verberg delete link
}
else
{
// delete functie waarbij een alert word weergegeven of je dit zeker wil
echo "<td><a class='link' href=\"javascript:delpage('$row->id','$row->title');\">Verwijder</a></td>";
echo '<br>';
echo "<td><a class='link' href=\"edit.php?id=$row->id\">Pas aan</a></td>";
echo '<br>';
echo "<td><a class='link' href=\"bekijk.php?id=$row->id\">Bekijk</a></td>";
}
echo "</tr>";
}
他们都链接到此页面:
<?php
session_start();
include '../includes/includes.php';
require('../includes/functions.php');
//make sure user is logged in, function will redirect use if not logged in
login_required();
//if logout has been clicked run the logout function which will destroy any active sessions and redirect to the login page
if(isset($_GET['logout'])){
logout();
}
?>
<html>
<title>Admin</title>
<script src="../ckeditor/ckeditor.js"></script>
<header><link rel="stylesheet" type="text/css" href="../css/style.css"></header>
<body>
<nav>
<ul>
<li><a href="index.php">Admin</a></li>
<li><a href="../index.php" target=_blank"">Bekijk site</a></li>
<li><a href="voegtoe.php">Toevoegen</a></li>
<li><a href="verwijder.php">Posts</a></li>
<li><a href="index.php?logout">Uitloggen</a></li>
</ul>
</nav>
</body>
</html>
<?php
//pak het ID van de pagina die aangepast moet worden
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$q = mysql_query("SELECT * FROM blog_posts WHERE id='$id'");
$row = mysql_fetch_object($q);
?>
<div id="bekijkwrap">
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $row->id;?>" />
<h1><?php echo $row->title;?></h1>
<p><?php echo $row->post;?></p>
</form>
</div>
答案 0 :(得分:0)
您的破解代码循环遍历mysql_fetch_assoc(),将$rows
变量设置为每次迭代的(关联)数组。
您的工作代码调用mysql_fetch_object(),为每次迭代撤回一个对象(类StdClass)。
您的损坏代码会尝试通过将$row
变量视为对象来引用该ID。这个变量不仅不存在(它应该是$rows
),而$rows
变量是一个关联数组,而不是一个对象。
因此,不是使用语法$foo->id
访问该值,而是必须像$foo['id']
那样访问它。
要解决此问题,请更改:
echo "<td><a class='link' href=\"/admin/bekijk.php?id=$row->id\">Lees meer</a>
要
echo "<td><a class='link' href=\"/admin/bekijk.php?id=" . $rows['id'] . "\">Lees meer</a>
您可以在上面的行中看到您在原始代码中引用$rows
并将其视为数组。这也是你必须对问题行做的事情。