如何更改URL ID?

时间:2017-02-08 16:12:05

标签: php backend

如果id高于db?

中的max id,我该如何更改URL id

如果我从数据库中获取id并在read more按钮中使用它,以使人们在id为不同url查询的新页面中读取数据?

- 真正的问题 我如何确保如果在db中只有4条新闻,如果你写了等等 newsTest.php?id = 5在浏览器中浏览器不会执行或返回到最大页面?

//read more button
<a href="newsTest.php?id=<?php echo $id ?>">Læs mere</a>


<?php
    include_once 'includes/db.php';
            $sql1 = "SELECT COUNT(id) AS total FROM rock_news "; 
            $result1 = $dbCon->query($sql1);
            $row1 = $result1->fetch_assoc();

            $total_pages = $row1["total"];
            $thisVar = $_GET['id'];

            if($total_pages >= $thisVar){
                echo "alt er godt";


    if(isset($_GET['id']) && !empty($_GET['id'])){

        $id = $_GET['id'];

        // mod sqlinjection
        $id = $dbCon->real_escape_string($id);

        $sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;

        $result = $dbCon->query($sql);

        if ($result->num_rows > 0) {
            $row = $result->fetch_object();
                $id = $row->id;
                $heading = utf8_encode($row->heading);
                $subheading = utf8_encode($row->subheading);
                $description = utf8_encode($row->description);
                $created = $row->created;
                $author = utf8_encode($row->author);

            $output .= $id . "<br>" . $heading . "<br>" . $description;




        };
        // udskriv output til bruger
       echo $output;
    };
 } else {
     $_GET['id'] = $total_pages ;
             echo "nothing";
 }
?>

我是php的新手

1 个答案:

答案 0 :(得分:0)

检查total_pages是否小于id,然后显示show total_pages id。

<?php
        include_once 'includes/db.php';
                $sql1 = "SELECT COUNT(id) AS total FROM rock_news "; 
                $result1 = $dbCon->query($sql1);
                $row1 = $result1->fetch_assoc();




        if(isset($_GET['id']) && !empty($_GET['id'])){
            $total_pages = $row1["total"];
            $thisVar = $_GET['id'];

                if($total_pages < $thisVar){
                    $thisVar=$total_pages
            $id = $thisVar;

            // mod sqlinjection
            $id = $dbCon->real_escape_string($id);

            $sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;