Problem solved, many thanks for participation
Asked
Active
Viewed 49 times
0
-
*he message is added to the database, but the entry does not appear in the guestbook* -- sorry, how did you show the data on the guestbook? – Bagus Tesa Jan 31 '19 at 01:08
-
ahh, that part, sorry i was lost. perhaps the reason was this part of your query `FROM users INNER JOIN kontakt`, i'm not exactly understand your table structure but i guess the anonymous user dont have an entry on the `users` table (which they shouldn't, so its correct already, perhaps) and [`INNER JOIN` only returns result that exists on both table that being joined](https://stackoverflow.com/q/13997365/4648586). i believe you can try to use `RIGHT JOIN` instead. hope it helps. – Bagus Tesa Jan 31 '19 at 01:14
1 Answers
0
$StrSQL2 = "SELECT users.benutzername as bn,
kontakt.betreff, kontakt.nachricht
FROM users INNER JOIN kontakt
ON users.userid = kontakt.userid_fk ORDER BY datum DESC";
$abfrage = $db->query($StrSQL2);
echo 'Es wurden '.$abfrage->num_rows.' Nachrichten gefunden!<br>';
while($ausgabe = $abfrage->fetch_object()){
echo '
<b>Datum:</b> '.$ausgabe->datum.' <br>
<b>Betreff:</b> '.$ausgabe->betreff.' <br>
<b>Nachricht:</b><br> '.$ausgabe->nachricht.' <br>
<b>Benutzer:</b><br> '.$ausgabe->bn.'<br><hr>';
}
The code that prints the guestbook entries above uses an INNER JOIN. INNER JOIN will only return results that exists on the two table being JOIN-ed
which in this case would be users and kontakt. You should use RIGHT JOIN to allow the kontakt retrieved even though it do not have users.
$StrSQL2 = "SELECT users.benutzername as bn,
kontakt.betreff, kontakt.nachricht
FROM users RIGHT JOIN kontakt
ON users.userid = kontakt.userid_fk ORDER BY datum DESC";
The next problem would be your Benutzer field empty as it originates from the users.benutzername as bn the user table. You could modify the code a little bit to show "Anonymous" if the particular value is null. Something like is_null might worth to try. Your code will be looked like:
while($ausgabe = $abfrage->fetch_object()){
echo '
<b>Datum:</b> '.$ausgabe->datum.' <br>
<b>Betreff:</b> '.$ausgabe->betreff.' <br>
<b>Nachricht:</b><br> '.$ausgabe->nachricht.' <br>
<b>Benutzer:</b><br> '. !is_null($ausgabe->bn) ? $ausgabe->bn : 'Anonymous' .'<br><hr>';
}
The code above uses ternary operator, the ? : pair.
Sorry, i was late to decide to write a proper answer for the question.
Bagus Tesa
- 979
- 1
- 18
- 37
-
@Sha, it still appear concatenated like that? hmm, weird. could you share your current code? sorry i was late to read your comment.. – Bagus Tesa Feb 02 '19 at 01:48