I hope you can help me. I have two tables. One table has the users who register on my website. The other table contains the messages that are sent via a form.
How can I transfer the user ID to the message so that I know which registered user wrote the message? Is that possible somehow, I don't know where to start?
My current status is that I only output the messages, but without USER ID or Username. I'd like to print this out as well.
<?php
if(isset($_POST['submit'])):
$betreff = $_POST['betreff'];
$nachricht = $_POST['nachricht'];
$absenden = $db->prepare("INSERT INTO kontakt (betreff,nachricht,datum) VALUES (?,?,NOW())");
$absenden->bind_param('ss',$betreff,$nachricht);
$absenden->execute();
endif;
$abfrage = $db->query("SELECT * FROM kontakt ORDER BY datum DESC");
echo 'Es wurden '.$abfrage->num_rows.' Nachrichten gefunden!<br>';
while($ausgabe = $abfrage->fetch_object()){
echo '
<b>Betreff:</b> '.$ausgabe->betreff.' <br>
<b>Nachricht:</b><br> '.$ausgabe->nachricht.' <br>
<hr>
';
}
include 'footer.html';
?>
It would have to work somehow via inner join, wouldn't it? Who can help?
Output Messages:
if(isset($_POST['submit'])):
$betreff = $_POST['betreff'];
$nachricht = $_POST['nachricht'];
$absenden = $db->prepare("INSERT INTO kontakt (betreff,nachricht,datum) VALUES (?,?,NOW())");
$absenden->bind_param('ss',$betreff,$nachricht);
$absenden->execute();
endif;
$abfrage = $db->query("SELECT * FROM kontakt ORDER BY datum DESC");
echo 'Es wurden '.$abfrage->num_rows.' Nachrichten gefunden!<br>';
while($ausgabe = $abfrage->fetch_object()){
echo '
<b>Betreff:</b> '.$ausgabe->betreff.' <br>
<b>Nachricht:</b><br> '.$ausgabe->nachricht.' <br>
<hr>
';
}
My structure:
Table users:
ID (Primary Key)
Username (Index) password created_at updated_at
Table contact:
id (primary key) topic message date