简体   繁体   English

在sqlite数据库的不同页面上显示具有相似ID的数据

[英]Display data with similar id on different page from sqlite database

Am new to php and i have successfully displayed data from sqlite database. 是PHP的新手,我已经成功显示了sqlite数据库中的数据。 The $catname is suppose to link to a page that show data from another table in the databse with the same id . 假定$catname链接到一个页面,该页面显示来自id相同的数据库中另一个table数据。 Below is the code: 下面是代码:

 <?php
require_once ("db.php");
$db = new MyDb();

    $sql =<<<EOF
SELECT * FROM addcategory ORDER BY catID DESC;
EOF;

    $ret = $db->query($sql);

    while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
        $catname = $row['catname'];
        $catdes = $row['catbrief'];
        $catimage = $row['catpic'];
        $catid = $row['catID'];

        echo "<div class=\"catDescription\">
<div class=\"catname\"><p><a href='search.php?category_id=$catid'>$catname</a>  </p></div>
    <div class=\"catImage\"><img src='".$catimage."'></div>
    <div class=\"catprof\"><p>$catdes</p></div>
    </div>";
    }

?> 

How do i display the data with thesame id with id from the other table 如何显示与另一个表中的idid的数据

I have tried this on the other page 我在另一页上尝试过

 <?php

$sql =<<<EOF
SELECT * FROM questions ORDER BY category_id DESC;
EOF;

$ret = $db->query($sql);

while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
    $catquestion = $row['question'];
    $catans = $row['answer'];


        echo "<div class=\"catDescription\">
<div class=\"catname\"><p>$catquestion</p></div>
    <div class=\"catprof\"><p>$catans</p></div>
    </div>";
}

?>

But it displays all data in the table and not data with similar id. 但是它将显示表中的所有数据,而不显示具有相似ID的数据。 Please what's wrong here and how do i fix this. 请在这里出什么问题以及如何解决此问题。

It seems you are missing the appropriate WHERE clause. 似乎您缺少适当的WHERE子句。 Your query should be something like: 您的查询应类似于:

$cat_id = (int)$_GET['category_id']; //cast to int to strip out sql injections
$sql =<<<EOF
SELECT * FROM questions WHERE category_id = {$cat_id} ORDER BY category_id DESC;
EOF;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM