dongruoqiong9017 2016-06-01 10:06
浏览 27

如果URL字符串值不在数据库中或有效,则重定向404?

I have a 'video' page that is passed a variable by the URL (video.php?id=VALUE). This id value is used throughout the page to pull in various details about the video - title, a still, year etc.

I want to display a 404/error if an incorrect value (/something not in the database) is entered into the string, and display the page with corresponding values pulled from the database if the value is correct

My code is currently structured like this:

<?php 
    $video = $_GET['video'];
    $videodata = $pdo->prepare("SELECT * FROM video WHERE siteurl = ? LIMIT 1");
    $videodata->execute([$video]);
    ?>

    <head>
     <meta charset="utf-8">
     <meta http-equiv="x-ua-compatible" content="ie=edge">

     <title>   
     <?php
     foreach ($videodata as $videodatas) {
     echo "{$videodatas['title']}</title>";

     echo "<meta property=\"og:title\" content=\"{$videodatas['title']}\"/>"; }?>
     <meta name="description" content="Description lives here">

     <meta name="viewport" content="width=device-width, initial-scale=1">
     <meta name="apple-mobile-web-app-capable" content="yes">

     <meta property="og:url" content="<?php echo "http://" . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];?>" />


     <link rel="apple-touch-icon" href="apple-touch-icon.png">

     <link rel="stylesheet" href="scripts/style.css">


    </head>

When this successfully matches it generates the page, listing the correct video title in the <title> tags and throughout the rest of the page.

However, when I'm testing an incorrect variable in the URL/something that's not in my database the page is spitting out a completely blank page that includes all of the raw HTML in the <title> tag.

Any help (even in the direction of a tutorial on how to achieve this) would be much appreciated.

EDIT:

I've tried writing some code as a test with the intention to replace the if and else statements when I have it working. The code doesn't work at the moment and I'm not sure where I'm going wrong...:

<?php 
$video = $_GET['video']; 
$videodata = $pdo->prepare("SELECT * FROM video WHERE site url =:siteurl LIMIT 1"); // check db for :siteurl value

$videodata->bindParam(':siteurl', $_GET['video'], PDO::PARAM_STR); // bind site url to $_Get Value...

$videodata->execute([$video]); // execute

if($videodata->rowCount()) { // If videodata [url string] returns data then... 
 echo '<b>hello world... this entry is there!</b>';
 } else {
 echo 'NO .... ITS NOT THERE';
 }
;
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败