What are some common pitfalls for beginners when trying to display form results on another page using PHP and MySQL?
One common pitfall for beginners when trying to display form results on another page using PHP and MySQL is not properly passing the form data from one page to another. To solve this, you can use sessions or URL parameters to carry the data over to the next page. Another pitfall is not properly querying the database to retrieve the form data for display on the next page. Make sure to use the correct SQL query and fetch the data correctly before displaying it.
// First page (form page)
<form action="display_results.php" method="post">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
// Second page (display_results.php)
<?php
session_start();
$username = $_POST['username'];
// Store form data in session
$_SESSION['username'] = $username;
// Retrieve form data from session
echo "Username: " . $_SESSION['username'];
// Query database to retrieve additional data
// Make sure to establish a connection to your database before running this query
$query = "SELECT * FROM users WHERE username = '$username'";
$result = mysqli_query($connection, $query);
// Fetch and display data
while($row = mysqli_fetch_assoc($result)) {
echo "Email: " . $row['email'];
}
?>