添加验证/如果其他语句公共类功能PHP
嗨即时尝试建立一个简单的博客使用PHP PDO,但我有点卡住验证/否则,因为什么曾经发生在无类凌乱的版本是它会说“这篇文章不存在”,但现在它只是显示空框的页面,所以我想知道如何添加if/else staements类使其工作,并显示消息时,id不是在数据库中匹配添加验证/如果其他语句公共类功能PHP
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然后它说这篇文章不存在,但目前它只是空白。
感谢所有帮助表示赞赏+这不是重复我找不到任何地方的任何答案!
回答:
检查查询后$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"; 
} 
回答:
你有2种选择:
重定向到显示错误,像
post_not_found.php什么的页面。用if/else语句包装整个html页面,if/else语句将根据您的条件输出不同的内容。
我建议你第一个选项去,你需要做的是检查是否有$post数据,如果没有的话,你做header("Location: post_not_found.php");
以上是 添加验证/如果其他语句公共类功能PHP 的全部内容, 来源链接: utcz.com/qa/267318.html

