从“结束”或“第一”值[重复]之后的数据库元素的按钮上一次和下一次加载

This question already has an answer here:

I show one video per page from category and with buttons next/previous user can navigate to next or previous id in database. The problem is that when I am on the first video of that category the button previous is still there and I can go one id previous which is another category. Same is with button next. When user is on last video of that category the button for next is still there and can click on it which will load next id from database ( next category ).

 if(isset($_GET['video_id']) && is_numeric($_GET['video_id']) && isset($_GET['video_cat_id']) && is_numeric($_GET['video_cat_id'])){

    $video_id = $_GET['video_id'];
    $video_cat_id = $_GET['video_cat_id'];    

// get next picture id
$result = $pdo->prepare('SELECT video_id FROM video WHERE video_id > :video_id ORDER BY video_id ASC LIMIT 1');
if($result){
    $result->execute(array(':video_id' => $video_id));
    //$result->execute(array(':video_cat_id' => $video_cat_id));
    if (($row = $result->fetch()) !== FALSE) {
       $next_id = $row['video_id'];
    }
}

// get previous picture id
$result = $pdo->prepare('SELECT video_id FROM video WHERE video_id < :video_id ORDER BY video_id DESC LIMIT 1');
if($result){
    $result->execute(array(':video_id' => $video_id));
    //$result->execute(array(':video_cat_id' => $video_cat_id));
    if (($row = $result->fetch()) !== FALSE) {
       $prev_id = $row['video_id'];
    }
}

  $result = $pdo->prepare("SELECT * FROM video WHERE video_id= :video_id LIMIT 1");
  if ($result->execute(array(':video_id'=>$_GET['video_id']))) 
  {       

   $prev_button = (isset($prev_id) && $prev_id>0)?'<a href="vcat-'.$video_cat_id.'/video-'.$prev_id.'.html"><i class="fa fa-arrow-left fa-3x"></i></a>':'';
   $next_button = (isset($next_id) && $next_id>0)?'<a href="vcat-'.$video_cat_id.'/video-'.$next_id.'.html"><i class="fa fa-arrow-right fa-3x"></i></a>':'';      
   if($row = $result->fetch())
   {
           // video goes here

What I saw is that in the query SELECT * FROM video WHERE video_id= :video_id LIMIT 1 I didn't select the category.

What I try is to make the query like this

$result = $pdo->prepare("SELECT * FROM video WHERE video_id= :video_id and video_cat_id = :video_cat_id LIMIT 1");
$result->bindParam(':video_id', $_GET['video_id'], PDO::PARAM_INT);
$result->bindParam(':video_cat_id', $video_cat_id, PDO::PARAM_INT);   
if ($result->execute())    
{
          // video

But is loading me blank page. What can be the actual problem here?

UPDATE:

I make this

// get next picture id
$result = $pdo->prepare('SELECT video_id FROM video WHERE video_id > :video_id and video_cat_id = :video_cat_id ORDER BY video_id ASC LIMIT 1');
if($result){
   $result->bindParam(':video_id', $_GET['video_id'], PDO::PARAM_INT);
   $result->bindParam(':video_cat_id', $video_cat_id, PDO::PARAM_INT);
    $result->execute();
    if (($row = $result->fetch()) !== FALSE) {
       $next_id = $row['video_id'];
    }
}

same for previous picture - added video_cat_id in query. Then this:

  $prev_button = '<a href="vcat-'.$video_cat_id.'/video-'.$prev_id.'.html"><i class="fa fa-arrow-left fa-3x"></i></a>':'';
  $next_button = '<a href="vcat-'.$video_cat_id.'/video-'.$next_id.'.html"><i class="fa fa-arrow-right fa-3x"></i></a>':'';      
  if(isset($prev_id) && $prev_id > 0) { echo $prev_button; }
  if(isset($next_id) && $next_id > 0) { echo $next_button; } 
  if($row = $result->fetch())
  {

I've got the error:

PHP Parse error: syntax error, unexpected ':'

</div>

Your "first" and "previous" sql queries should include and video_cat_id = :video_cat_id that way you won't be able to go back or forward into another category.

Then, this code is not an "if" statement...

$prev_button = (isset($prev_id) && $prev_id>0)?'<a href="vcat-'.$video_cat_id.'/video-'.$prev_id.'.html"><i class="fa fa-arrow-left fa-3x"></i></a>':'';
$next_button = (isset($next_id) && $next_id>0)?'<a href="vcat-'.$video_cat_id.'/video-'.$next_id.'.html"><i class="fa fa-arrow-right fa-3x"></i></a>':'';      
if($row = $result->fetch())
{
       // video goes here

Define what your button should look like and the parameters it should use. like this...

$prev_button = '<a href="vcat-'.$video_cat_id.'/video-'.$prev_id.'.php"><i class="fa fa-arrow-left fa-3x"></i></a>':'';
$next_button = '<a href="vcat-'.$video_cat_id.'/video-'.$next_id.'.php"><i class="fa fa-arrow-right fa-3x"></i></a>':'';

Then use an if statement to decide IF you should show them or not. Basically...
If there is a previous video in the category - show the button.
If there is a next video in the category - show the button.

if(isset($prev_id) && $prev_id > 0) { echo $prev_button; }
if(isset($next_id) && $next_id > 0) { echo $next_button; }     
if($row = $result->fetch())
{
       // video goes here

Hope this helps.

Happy Coding!

Update:

I completely missed this... Your buttons are redirecting to an .html page. An .html or.htm page cannot process php. You will need to change your pages to .php and change the code for $prev_button = and $next_button = to point to a .php page as I did above.