Hi. I've created an email system where people can sign up and receive emails. I want to make a link that says: "Trouble viewing email? View online!" or something like that. I've made it so that the email sent to people is saved to a database, and then it will echo the html that is saved. My problem is, that page displays the latest email, not the one that they clicked the link to see. How do I get the email that is in the row with the unique id in the url? (The page will be on something like mysite.com/viewemail.php?id=1&email=me@example.com) My code now is:

<?php
$email_id = $_GET['id'];
$email = $_GET['email'];

  $dbc = mysqli_connect('localhost', 'my_username', 'my_password', 'my_database')
    or die('Error: Could not connect to database');

  $query = "SELECT * FROM my_table";
  $result = mysqli_query($dbc, $query)
    or die('Error: Could not connect to database');

  while ($row = mysqli_fetch_array($result)){
    $body = $row['body'];
    $subject = $row['subject'];
  } 

  mysqli_close($dbc);
?>
<html>
<head>
<title><?php echo $subject; ?></title>
</head>
<body>
<?php echo $body; ?>
</body>
</html>

Again, I want to have the PHP get the HTML from the row with the id in the url. So, if the page is mysite.com/viewemail.php?id=3 it'll display the third email.

Thanks!

Recommended Answers

All 5 Replies

$query = "SELECT * FROM my_table WHERE email_id = $email_id";

This should work if you built the specific email id in the same table.

$query = "SELECT * FROM my_table WHERE email_id = $email_id";

This should work if you built the specific email id in the same table.

I tried it, and it works! Thanks!

Solved ?

Yes. Thanks!

Mark this as 'Solve Thread' then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.