nbajpai nbajpai - 4 months ago 9
MySQL Question

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

Category Table


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

INSERT INTO category (contents, id) VALUES
('food', 1),
('electronics', 2),
('grocery', 3);


Content details Table


CREATE TABLE IF NOT EXISTS content_details (
cd_id smallint(6) NOT NULL,
contents varchar(50) NOT NULL,
details text NOT NULL,
id smallint(6) NOT NULL,
KEY link_id (id)

INSERT INTO content_details (cd_id, contents, details, id) VALUES
(1, 'food', 'Food is any substance consumed..', 1),
(2, 'electronics', 'Electronics is the science..', 2),
(3, 'grocery', 'A grocery store is a retail store....', 3);


When i open my main file index.php it must show contents from category table in link form like :

Id contents
1 Food
2 Electronics
3 Grocery


Now my question is that when i click on "food" link it should open "details" from content_details table and show them on new file i.e. view.php here is my code for 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();
?>


So, i want the new page should display the "details" content respective to the link open.
For example when i click on food it should open food description from content_details table. For grocery it should open grocery details and so on

Code for my view.php which I have written is given below. Its not working I don't know where i am wrong:

<?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>";

?>

Answer

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