Hallo Claus,
da Du geschrieben hast, das Dein Anbieter PHP unterstützt, setze ich daher vorraus, das er Dir auch eine MySQL Datenbank zur Verfügung stellt.
Daher einmal folgende Möglichkeit für ein einfaches Gästebuch in PHP mit MySQL Unterstützung:
Erstelle in Deiner MySQL Datenbank eine Tabelle z.B. mit
PHPMyAdmin und folgendem SQL-Befehl :
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
|
CREATE TABLE gast (
nr int(11) NOT NULL auto_increment,
name varchar(150),
email varchar(255),
open enum('0','1'),
hp varchar(255),
inhalt text,
kommentar text,
datum int(11),
PRIMARY KEY (nr)
);
|
Anschließend erstellst Du eine Datei namens gaestebuch.php mit folgendem Inhalt:
|
PHP Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
<?php
// Systemeinstellungen
$id = ""; // Benutzername zum MySQL Server
$pw = ""; // Passwort zum MySQL Server
$host = "localhost"; // Host ("localhost" oder "IP-Adresse" oder "Domain")
$database = ""; // Name der Datenbank
$table = "gast"; // Name der Tabelle
// Einstellungen Ende
// Verbindung zum Server und Datenbank
$conn_id = mysql_connect($host,$id,$pw);
mysql_select_db($database,$conn_id);
// Speichert einen neuen Eintrag in der Datenabank ab
if ($action=="save") {
$datum = time();
$inhalt = $inhalt;
$name = ($name) ? $name : "unbekannt";
mysql_query("insert into $table (name,email,open,hp,inhalt,datum) VALUES ('$name','$email','$open','$hp','$inhalt','$datum')");
header("Location: ".$PHP_SELF);
}
// Hier beginnt die HTML Ausgabe
echo "<html>";
echo "<h3>Mein Gästebuch</h3>";
if($action=="neu") {
?>
<table><form action="<?php echo $PHP_SELF; ?>" method=post><tr>
<input type="hidden" name="action" value="save">
<td>name:</td>
<td><input type=text name=name></td>
</tr><tr>
<td>Email:</td>
<td><input type=TEXT name=email></td>
</tr><tr>
<td> </td>
<td><input type=checkbox name=open checked value=1>
<font size=1>Soll die Emailadresse später im Gästebuch angezeigt werden?</td>
</tr><tr>
<td>Homepage:</td>
<td><input type=text name=hp></td>
</tr><tr>
<td>Meinung:</td>
<td><textarea name="inhalt" rows="6" cols="25"></textarea></td>
</tr><tr>
<td> </td>
<td><input type=submit value="In das Gästebuch eintragen"></td></form>
</tr></table>
<?php
// Ausgabe aller Einträge aus dem Gästebuch
} else {
$query = "select * from $table order by datum desc";
$result = mysql_query($query);
if (mysql_num_rows($result)) {
while($row=mysql_fetch_array($result)) {
$name = $row['name'];
$open = $row['open'];
$email = $row['email'];
$hp = $row['hp'];
$inhalt = $row['inhalt'];
$kommentar = $row['kommentar'];
$datum = date("d.m.y",$row['datum']);
echo "Eintrag vom: ".$datum."<br>";
echo "User: ".$name;
if($open && $email) echo " (".$email.")";
if($hp) echo " (".$hp.")";
echo "<br>Meinung: ".$inhalt;
if($kommentar) echo "Unser Kommentar: ".$kommentar."<br>";
echo "<hr>";
flush();
}
} else {
echo "Bis jetzt gab es keine Einträge";
}
}
echo "<p><a href='".$PHP_SELF."?action=neu'>Neuer Eintrag</a>";
echo " - <a href='".$PHP_SELF."'>Zum Gästebuch</a>";
echo "<html>";
?>
|
Vergesse am Anfang der Datei nicht, die Daten (Benutzer,Passwort,Datenbankname) für Deine Datenbank einzutragen.
Solltest Due keine Datenbank haben oder Schwierigkeiten haben, die Tabelle anzulegen weil der Vorgang zu kompliziert ist, dann melde Dich hier noch einmal und ich poste Dir eine Alternative Möglichkeit, wofür man keine Datenbank benötigt.
Der Vorteil an einer MySQL-Datenbank besteht darin, das hier sehr große Datenmengen in kürzester Zeit verarbeitet werden können.