我有这个文件在db中显示博客条目我需要做的是点击阅读更多时获取id并显示该特定id的数据..
<?php
$results = $mysqli->query("SELECT * FROM blog ORDER BY id ASC");
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<div class="product-thumb"><img src="'.$obj->blog_img_name.'"></div>';
echo '<div class="product-content"><h3>'.$obj->blog_name.'</h3>';
echo '<div class="product-desc"><b>'.$obj->blog_date.'</b>'.'</div>';
echo '<div class="product-desc">'.$obj->blog_desc.'</div>';
echo '<div class="product-info">';
echo '<a href="sunblog1.php?action=add&id=$id">Read more</a>';
echo '</div></div>';
echo '</div>';
}
}
?>
出于上述目的,我将以下文件命名为
sunblog1.php
<?php
if (isset($_GET['id']) && isset($_GET['action'])) {
$blog_id= $_GET["id"]; //the blog id from the URL
$action= $_GET["action"]; //the action from the URL
}
?>
<?php
$results = $mysqli->query("SELECT * FROM blog WHERE id =$blog_id");
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<div class="product-content"><h3>'.$obj->blog_name.'</h3>';
echo '<div class="product-desc"><b>'.$obj->blog_date.'</b>'.'</div>';
echo '<div class="product-desc">'.$obj->blog_desc.'</div>';
echo '<div class="product-desc">'.$obj->blog_main.'</div>';
echo '<div class="product-info">';
echo '</div></div>';
echo '</div>';
}
}
?>
但它只给我一个空白页....
任何人都可以帮忙....
答案 0 :(得分:0)
它为您提供空白页面,因为它不会从您的链接获取ID
将您的$id
更改为$obj->id
<?php
$results = $mysqli->query("SELECT * FROM blog ORDER BY id ASC");
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<div class="product-thumb"><img src="'.$obj->blog_img_name.'"></div>';
echo '<div class="product-content"><h3>'.$obj->blog_name.'</h3>';
echo '<div class="product-desc"><b>'.$obj->blog_date.'</b>'.'</div>';
echo '<div class="product-desc">'.$obj->blog_desc.'</div>';
echo '<div class="product-info">';
echo '<a href="sunblog1.php?action=add&id='.$obj->id.'">Read more</a>';
echo '</div></div>';
echo '</div>';
}
}
?>
答案 1 :(得分:0)
这可能有很多原因。首先制作$ blog_id =(int)$ _ GET [&#34; id&#34;];避免mysql注入。 (不是在这种情况下的原因,但无论如何有用的做法)。首先尝试添加echo&#34; OK&#34 ;;在代码的最后(可能你稍后会有一些致命的错误)。如果显示ok,则添加此行
echo $result->num_rows;
之后
$results = $mysqli->query("SELECT * FROM blog WHERE id =$blog_id");
这将显示此查询返回的行数。如果它为零,则在db中查找,可能是您传递了错误的id。