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

时间:2016-07-11 09:25:30

标签: php html mysql sql-server

类别表

  

CREATE TABLE IF NOT EXISTS类别(     contents varchar(50)NOT NULL,     id smallint(6)NOT NULL AUTO_INCREMENT,     PRIMARY KEY(id)

     

INSERT INTO类别(内容,ID)VALUES   (' food',1),   (' electronics',2),   ('杂货店',3);

内容详细信息表

  

创建表,如果不是exISTS content_details(     cd_id smallint(6)NOT NULL,     contents varchar(50)NOT NULL,     详细信息文本NOT NULL,     id smallint(6)NOT NULL,     KEY link_id(id)

     

INSERT INTO content_details(cd_id,contents,details,id)VALUES   (1,'食物','食物是消耗的任何物质..',1),   (2,'电子','电子是科学..',2),   (3,'杂货店',#39;杂货店是零售店......',3);

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

Id     contents
1      Food 
2      Electronics 
3      Grocery

现在我的问题是当我点击"食物"链接它应该打开" 详细信息"来自content_details表并在新文件中显示它们,即 view.php 这里是我的index.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();
?>

所以,我想新页面应该显示&#34;详细信息&#34;链接打开的内容。 例如,当我点击食物时,它应该从content_details表中打开食物描述。对于杂货店,它应该打开杂货店的详细信息等等

我写的 view.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>";

?>

2 个答案:

答案 0 :(得分:0)

View.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();
?>

答案 1 :(得分:0)

view.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>";

    ?>