创建动态链接而不创建新的.php页面/文件

创建动态链接而不创建新的.php页面/文件,php,mysql,dynamic,hyperlink,Php,Mysql,Dynamic,Hyperlink,我是PHP新手,我正在寻找在不创建新的.PHP页面/文件的情况下创建动态链接的可能方法 因此,我正在编写一个博客,对文章进行分类。A类、B类、C类等 还有我的桌子 id int(11) title varchar(255) post_category varchar(50) post_content text num_comments int(11) date_posted date 这就是帖子的分类设置 foreach ($category as $cat=>$value) {

我是PHP新手,我正在寻找在不创建新的.PHP页面/文件的情况下创建动态链接的可能方法

因此,我正在编写一个博客,对文章进行分类。A类、B类、C类等

还有我的桌子

id int(11)
title varchar(255)
post_category varchar(50)
post_content text
num_comments int(11)
date_posted date
这就是帖子的分类设置

foreach ($category as $cat=>$value) {
    $post_category = "";
    $post_category .= $value." ";
}
带有复选框

<input type="checkbox" name="category[]" value="CategoryA"> CategoryA 
<input type="checkbox" name="category[]" value="CategoryB"> CategoryB
<input type="checkbox" name="category[]" value="CategoryC"> CategoryC
在index.php中,我需要知道如何创建动态链接,如Category A、Category B等

这些链接根据帖子的类别包含帖子,而不创建CategoryA.php CategoryB.php等文件


任何帮助都将不胜感激

创建一个名为category.php的php文件。然后使用get变量传递您感兴趣的类别。因此,例如category.php?cat=A将获得类别A。如果需要更多信息,可以像echo$\u get[cat]一样读取此变量,请参见:

在sql中,您需要选择如下类别:

SELECT * FROM `your_table_name` WHERE `post_category` like '%'".$cat."'%'  

根据您的PHP版本,您应该在用作mysql查询的任何输入周围使用mysql\u real\u escape\u字符串

在您的博客页面上创建一个链接,其中包含:

 while ($row = mysqli_fetch_array($posts)) {
     // your code to display your content here
     // this ill create a link with a GET request and is passed with the post ID
     echo '<a href="category.php?id='.$row['category']'"><button class="read-more">Read More
     </button>  </a>'; 
 }
缺点是你的内容没有永久链接。它总是这样的
category.php?id=SomeRandomNumber。但是可能会有办法解决这个问题。

您的示例很容易受到SQL注入攻击。像这样,伙计?回声';更像CategoryX,然后在category.php文件中执行$\u GET代码
// get post category from GET
$cat = $_GET['category'];
$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die('Error connecting to server');
$query = "SELECT * FROM posts WHERE category = '$cat'";
$posts = mysqli_query($dbc,$query);
while ($row = mysqli_fetch_array($posts)) {
    // your code to display your single post content here                   
}