我正在尝试使用PHP PDO构建一个简单的博客,但我在验证方面有点困惑/如果其他因为过去曾经发生在无类别的凌乱版本中它会说&#34 ;这篇文章不存在"但是现在它只显示带有空框的页面,所以我想知道如何将if / else语句添加到类中以使其工作并在id不是数据库中匹配的时显示消息
public function fetch_data($pid){
try{
global $pdo;
$query = $pdo->prepare('SELECT * FROM post where post_id = ? order by post_date desc');
$query->BindValue(1,$pid);
$query->execute();
return $query->fetch();
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
这是代码的公共函数位,而article.php页面代码是:
<?php
include_once('functions/main.php');
$post = new Main;
$check = new Main;
$check_login = $check->logged_in();
if(isset($_GET['id'])){
$pid = $_GET['id'];
$post = $post->fetch_data($pid);
$query = $pdo->prepare("UPDATE post SET post_views = post_views + 1 WHERE post_id = ?");
$query->execute(array($pid));
?>
<html>
<head>
<title><?php echo $post['post_title'];?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.customimage{
background: url('<?php echo $post['post_image'];?>') !important;
}
</style>
</head>
<body>
<div class="pusher">
<!-- Site content !-->
<div class="ui inverted vertical masthead center aligned segment purple customimage">
<div class="ui text">
<h1 class="ui inverted header">
<?php echo $post['post_title'];?></h1>
<br>
<div class="ui black inverted label"> <i class="calendar icon"></i><?php echo $post['post_date'];?></div><div class="ui black inverted label"><i class="user icon"></i> <?php echo $post['post_author'];?></div><div class="ui black inverted label"><i class="unhide icon"></i> <?php echo $post['post_views']?></div>
</div>
</div>
<div class="ui divider hidden"></div>
<div class="ui container">
<div class="ui segments">
<div class="ui segment purple">
<h1 class="ui header">
<div class="content">
<?php echo $post['post_title'];?>
</div>
</h1>
</div>
<div class="ui segment">
<?php echo $post['post_content'];?>
</div>
<div class="ui secondary segment">
<button class="ui labeled icon button">
<i class="left arrow icon"></i>
Return to Posts</button>
</div>
</div>
</div>
</div>
</body>
</html>
<?php
}else{
header('Location:index.php');
}
?>
我无法弄清楚如何制作它,以便当你去?id = 876799然后说文章不存在但目前它只是空白。
答案 0 :(得分:1)
在查询后和显示结果时检查$post
的值。
$post = $post->fetch_data($pid);
if ($post) {
$query = $pdo->prepare("UPDATE post SET post_views = post_views + 1 WHERE post_id = ?");
$query->execute(array($pid));
} else {
display_post_not_found($pid);
exit();
}
?>
<html>
...
</html>
在display_post_not_found()
函数(您必须编写)中,您可以显示有关错误的信息页面,或者只是重定向到某个地方。
完整代码:
<?php
include_once('functions/main.php');
$main = new Main;
$check = new Main;
$check_login = $check->logged_in();
if(isset($_GET['id'])){
$pid = $_GET['id'];
$post = $main->fetch_data($pid);
if ($post) {
$query = $pdo->prepare("UPDATE post SET post_views = post_views + 1 WHERE post_id = ?");
$query->execute(array($pid));
} else {
display_post_not_found($pid);
exit();
}
?>
<html>
<head>
<title><?php echo $post['post_title'];?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.customimage{
background: url('<?php echo $post['post_image'];?>') !important;
}
</style>
</head>
<body>
<div class="pusher">
<!-- Site content !-->
<div class="ui inverted vertical masthead center aligned segment purple customimage">
<div class="ui text">
<h1 class="ui inverted header">
<?php echo $post['post_title'];?></h1>
<br>
<div class="ui black inverted label"> <i class="calendar icon"></i><?php echo $post['post_date'];?></div><div class="ui black inverted label"><i class="user icon"></i> <?php echo $post['post_author'];?></div><div class="ui black inverted label"><i class="unhide icon"></i> <?php echo $post['post_views']?></div>
</div>
</div>
<div class="ui divider hidden"></div>
<div class="ui container">
<div class="ui segments">
<div class="ui segment purple">
<h1 class="ui header">
<div class="content">
<?php echo $post['post_title'];?>
</div>
</h1>
</div>
<div class="ui segment">
<?php echo $post['post_content'];?>
</div>
<div class="ui secondary segment">
<button class="ui labeled icon button">
<i class="left arrow icon"></i>
Return to Posts</button>
</div>
</div>
</div>
</div>
</body>
</html>
<?php
}else{
header('Location:index.php');
}
function display_post_not_found($pid) {
echo "Post $pid could not be found";
}
答案 1 :(得分:0)
你有两个选择:
重定向到显示错误的页面,例如post_not_found.php
或其他内容。
使用if / else语句包装您现在拥有的整个html页面,该语句将根据您的条件输出不同的内容。
我建议你选择第一个选项,你需要做的是检查$post
是否有数据,如果它没有header("Location: post_not_found.php");