G
Gelöschtes Mitglied 36401
Guest
Weil der ganze "Mist" ja so schwer ist, habe ich das mal in 15 Minuten zusammengeschrieben. Das ist noch nicht optimal, aber vielleicht verstehst du es ja so besser. Aber bitte jetzt nicht nur Copy/Paste, sondern versuche zu verstehen, was da passiert:
Tabelle
index.php
add.html
save.php
style.css
Tabelle
Code:
CREATE TABLE `gb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`date` datetime NOT NULL,
`message` text COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`ip` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
PHP:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gästebuch</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<a class='link' href="add.html">Eintrag hinzufügen</a><br><br>
<table>
<tr>
<th>Datum</th>
<th>Name</th>
<th>eMail</th>
<th>Nachricht</th>
<th>IP</th>
</tr>
<?php
$mysqli = mysqli_connect('localhost', 'user', 'passwort', 'db');
if (mysqli_connect_errno())
die ("Connect failed: " . mysqli_connect_error());
mysqli_set_charset($mysqli, "utf8");
$query = "Select
`id`,
DATE_FORMAT(`date`, '%d.%m.%Y, %h:%i') as mydate,
`name`,
`email`,
`message`,
`ip`
from
`gb`
order by
`date` desc";
$result = mysqli_query($mysqli, $query)
or die ("MySQL-Error: " . mysqli_error($mysqli));
if (mysqli_num_rows($result))
{
$i = 0;
while ($row = mysqli_fetch_assoc($result))
{
$style = ($i++ % 2) ? "class='light'" : "class='dark'";
echo "<tr>
<td $style>" . $row['mydate'] . "</td>
<td $style>" . htmlentities($row['name'], ENT_QUOTES, "UTF-8") . "</td>
<td $style>" . htmlentities($row['email'], ENT_QUOTES, "UTF-8") . "</td>
<td $style>" . nl2br(htmlentities($row['message'], ENT_QUOTES, "UTF-8")) . "</td>
<td $style>******************</td>
</tr>";
}
}
else
echo "<tr>
<td colspan='5'>Keine Einträge in der Datenbank vorhanden
</tr>\n";
?>
</table>
</body>
</html>
Code:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gästebuch</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h1>Eintrag hinzufügen</h1>
<form action="save.php" method="post">
<fieldset>
Name:<br>
<input type="text" name="name" required><br>
eMail:<br>
<input type="email" name="email" required><br>
Eintrag:<br>
<textarea name="message" cols='80' rows='10' required></textarea><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
</body>
</html>
PHP:
<?php
if (isset($_POST['name']))
{
$mysqli = mysqli_connect('localhost', 'user', 'passwort', 'db');
if (mysqli_connect_errno())
die ("Connect failed: " . mysqli_connect_error());
mysqli_set_charset($mysqli, "utf8");
$query = "Insert
into
`gb`
(`date`, `name`, `email`, `message`, `ip`)
values
(now(), '%s', '%s', '%s', '%s')";
$query = sprintf($query, mysqli_real_escape_string($mysqli, $_POST['name']),
mysqli_real_escape_string($mysqli, $_POST['email']),
mysqli_real_escape_string($mysqli, $_POST['message']),
$_SERVER['REMOTE_ADDR']);
$result = mysqli_query($mysqli, $query)
or die ("MySQL-Error: " . mysqli_error($mysqli));
header("Location: index.php");
}
?>
Code:
html, body {
margin: 10px;
padding: 10px;
background-color: #eee;
}
main {
width: 90%;
padding: 0px;
margin: 0px auto 20px auto;
}
h1 {
text-align: center;
}
table {
width: 90%;
margin: 0px auto;
border-collapse: collapse;
}
td, th {
padding: 5px;
text-align: left;
border: 1px solid #000000; border-collapse: collapse;
}
th { bachground.color: #c0c0c0; }
.dark {
background-color: #aaa;
}
.light {
background-color: #ccc;
}
.form_wrapper {
width: 90%;
margin: 0px auto;
}
input#nic_name {
width: 100%;
max-width: 300px;
}
textarea {
display: block;
width: 100%;
height: 15vh;
margin: 0px auto;
}