我想通过单击链接从其他表中调用数据


I want to call data from other table by clicking the link

类别表

如果不存在,则创建表 类别 ( 目录 瓦尔查尔(50) 不为空, id smallint(6) 不是空AUTO_INCREMENT, 主键 (ID)

插入到类别(内容、ID)值 ("食物",1), ("电子",2), ("杂货店",3);

内容详细信息表

如果不存在,则创建表 content_details ( cd_id SMALLINT(6) 不为空, 目录 瓦尔查尔(50) 不为空, 详细信息文本不为空, id smallint(6) 不为空, 密钥link_id (ID)

插入到content_details(cd_id、内容、详细信息、ID)值中 (1,"食物","食物是任何消耗的物质......",1), (2,"电子学","电子学是科学......",2), (3,"杂货店","杂货店是零售店......",3);

当我打开主文件索引时.php它必须以链接形式显示类别表中的内容,例如:

Id     contents
1      Food 
2      Electronics 
3      Grocery

现在我的问题是,当我单击"食物"链接时,它应该从表中打开content_details"详细信息"并将它们显示在新文件(即视图)上.php这是我的索引代码.php:

<?php
//Open a new connection to the MySQL server <br>
$mysqli = new mysqli('localhost','root','','article_management'); <br>
//Output any connection error
if ($mysqli->connect_error) { 
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
} 
//MySqli Select Query
$results = $mysqli->query("SELECT id, contents FROM category");
print '<table border="1">'; 
while($row = $results->fetch_assoc()) { 
    $id = $row['id'];
    $contents = $row['contents'];
    print '<tr>';
    print '<td>'.$row["id"].'</td>';
    echo '<td><a href="view.php?id=' . $id . '">' . $contents . '</a></td>';
    print '</tr>'; 
}  
print '</table>';
// Frees the memory associated with a result <br>
$results->free();
// close connection 
$mysqli->close();
?>

因此,我希望新页面应显示与打开的链接相关的"详细信息"内容。例如,当我单击食物时,它应该从content_details表中打开食物描述。对于杂货店,它应该打开杂货店的详细信息等等

下面给出了我的观点代码.php下面给出了我写的。它不起作用我不知道我错在哪里:

<?PHP 
$id = $_GET['id'];
$result = mysqli_query("SELECT details, id 
FROM content_details 
WHERE id = $id");

echo "<table width=100%>
<tr>
<th>Content Details</th>
<th>Numbers</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <a href='#'>" . $row['details'] . "</a> </td>";
echo "<td>" . $row['id'] .  "</td>";
echo "</tr>";
}
echo "</table>";
?>

视图.php

<?php
//Open a new connection to the MySQL server <br>
$mysqli = new mysqli('localhost','root','','article_management'); <br>
//Output any connection error
if ($mysqli->connect_error) { 
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
} 
$category_id = (int)$_GET['id'];
//MySqli Select Query
$results = $mysqli->query("SELECT details FROM content_details where id = $category_id");
print '<table border="1">'; 
while($row = $results->fetch_assoc()) { 
    $details = $row['details'];
    print '<tr>';
    echo '<td>' . $details . '</td>';
    print '</tr>'; 
}  
print '</table>';
// Frees the memory associated with a result <br>
$results->free();
// close connection 
$mysqli->close();
?>

视图的代码.php应该如下所示,根据您的表结构:

    <?PHP 
    $id = $_GET['id'];
    $con=mysqli_connect("hostname","my_user","my_password","my_db");
    // Check connection
    if (mysqli_connect_errno())
    {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con, "SELECT content_details, id 
    FROM content_details WHERE id = $id");

    echo "<table width=100%>
    <tr>
    <th>Content Details</th>
    <th>Numbers</th>
    </tr>";
    while($row = mysqli_fetch_array($result, MYSQLI_NUM))
    {
    echo "<tr>";
    echo "<td> <a href='#'>" . $row['details'] . "</a> </td>";
    echo "<td>" . $row['id'] .  "</td>";
    echo "</tr>";
    }
    echo "</table>";
    ?>