2017-12-27 820 views
0

嗨即時嘗試建立一個簡單的博客使用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然後它說這篇文章不存在,但目前它只是空白。

感謝所有幫助表示讚賞+這不是重複我找不到任何地方的任何答案!

+0

哪裏的if/else問題? –

+0

@devlincarnate @devlincarnate我一直在試圖把它們放進去,但我不知道在哪裏或如何做到這一點,所以我問專業人員,所以我可以學習未來,即時通訊非常非常新的PDO – Jack

+0

不要像手工構建JSON 。如果你想回顯JSON,使用'echo json_encode(array('error'=> array('text'=> $ e-> getMessage())));' – Barmar

回答

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"; 
} 
0

你有2種選擇:

  1. 重定向到顯示錯誤,像post_not_found.php什麼的頁面。

  2. 用if/else語句包裝整個html頁面,if/else語句將根據您的條件輸出不同的內容。

我建議你第一個選項去,你需要做的是檢查是否有$post數據,如果沒有的話,你做header("Location: post_not_found.php");

+0

我該如何讓它們工作,因爲目前我的頭重定向只適用於其只是post.php不post.php?id = 857484,我想要什麼,以及它在真正凌亂的2012 php中做的是它說「Article Not Found」,但現在如果我做post.php?id = 857484它只是顯示佈局和所有,但與空白框,我希望它顯示一個錯誤消息,但我不知道如何使我的當前代碼和PDO發生 – Jack