【问题标题】:Add validation/if else statements to public class functions in PHP向 PHP 中的公共类函数添加验证/if else 语句
【发布时间】:2018-06-08 10:38:35
【问题描述】:

我正在尝试使用 PHP PDO 构建一个简单的博客,但我在验证/if else 上有点卡住了,因为在无类混乱版本中曾经发生的事情是它会说“这篇文章不存在”但现在它只显示带有空框的页面,所以我想知道如何将 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 时,它会说该文章不存在,但目前它只是空白。

【问题讨论】:

  • 有问题的 if / else 在哪里?
  • @devlincarnate 我一直在尝试把它们放进去,但我看不到在哪里或如何做,所以我问了专业人士,这样我就可以为未来学习,而且我对 PDO 非常陌生
  • 不要那样手工构建 JSON。如果要回显 JSON,请使用 echo json_encode(array('error' =&gt; array('text' =&gt; $e-&gt;getMessage())));

标签: php function class if-statement pdo


【解决方案1】:

你有两个选择:

  1. 重定向到显示错误的页面,例如post_not_found.php 之类的。

  2. 使用 if/else 语句包装您现在拥有的整个 html 页面,该语句将根据您的条件输出不同的内容。

我建议你用第一个选项,你需要做的是检查$post是否有数据,如果没有,那么你做header("Location: post_not_found.php");

【讨论】:

  • 我如何让它们工作,因为目前我的标题重定向只有在它只是 post.php 而不是 post.php?id=857484 以及我想要什么以及它曾经在真正混乱的 2012 年做什么时才有效php 是不是说“找不到文章”,但现在如果我做 post.php?id=857484 它只显示布局和除了空白框之外的所有内容,我希望它显示错误消息,但我不知道如何做到这一点使用我当前的代码和 PDO
【解决方案2】:

查询后和显示结果时检查$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";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多