What is the best practice for linking and displaying additional information based on a specific ID in PHP?
When linking and displaying additional information based on a specific ID in PHP, it is best practice to use a database to store the information and retrieve it using SQL queries. You can create a link with the specific ID as a parameter, then fetch the information from the database based on that ID and display it on a new page.
// Link to pass the specific ID
<a href="additional_info.php?id=123">View Additional Info</a>
// additional_info.php
$id = $_GET['id'];
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Fetch additional information based on ID
$sql = "SELECT * FROM table_name WHERE id = $id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Additional Information: " . $row["additional_info"];
}
} else {
echo "No additional information found.";
}
$conn->close();
Related Questions
- What are the potential pitfalls of setting cookies in PHP before or after HTML output, as demonstrated in the provided code snippets?
- What is the difference between calling a PHP function from an HTML tag and a JavaScript function?
- What are the advantages and disadvantages of performing date calculations directly in MySQL queries versus using PHP for date manipulation?