Zum Abschluss wurden die CSS-Einstellungen erweitert und der Header in HTML verlagert. Ein Basis-Layout mit DIV-Container wird verwendet. Mit

 $eintrag=mysqli_real_escape_string($conn,$_REQUEST['eintrag']); wird die Eingabe aus dem Formular überprüft und für die Datenbank aufbereitet.

 Download

 

<!DOCTYPE html>

<html lang="de" xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset='utf-8'/>

<title>G&auml;stebuch</title>

<style>

    #rahmen

    {

        margin:0 auto;

        width:60em;       

    }

    #sidebar

    {

        width:10em;

        min-height:20em;

        float:left;

        background: #cfc;

    }

    #content

    {

        width:48em;

        float:right;

        padding: 1em;

    }

    .bigtab

    {

        border:1px solid black; border-collapse:collapse; background:#ccc; width:20em; margin-bottom:1em;

    }

    .smalltab

    {

        border:1px solid black; border-collapse:collapse; background:#ccc; width:10em;

    }

</style>

</head><body>

<div id="rahmen">

    <div id="sidebar">Sidebar</div>

    <div id="content">       

<?php

 

//globale Variablen

$mysqldatenbank="gastbuchDB";

$mysqlusername="root";

$mysqlpasswort="";

$gastbuchtitel= "Mein G&auml;tebuch";;

 

//Ablaufsteuerung

if(isset($_REQUEST['subject']))$sub=$_REQUEST['subject']; else $sub="";

switch($sub)

{

    case "":start(); break;

    case "install":install(); break;

    case "save":speichern(); break;

    case "admin":login(); break;

    case "check":check(); break;

    case "del":loeschen(); break;     

}

 

 

// ************ Funktionen *************

function check()

{

    $passwort=$_REQUEST['pw'];

  

    if($passwort!="geheim")

    {

        start();

    }

    else

    {

        editieren();

    }

}

 

function editieren()

{

    global $gastbuchtitel;

 

    $conn=inidb();

    echo("G&auml;stebuch von $gastbuchtitel"); 

 

    $result = mysqli_query($conn, "SELECT * FROM gastbuch ORDER BY nummer DESC");

    if (mysqli_num_rows($result) > 0)

    {

        while($row = mysqli_fetch_assoc($result))

        {

            $s="<table class='bigtab'>";

            $s.="<tr><td>Name: ".$row['name']."</td></tr>";

            $s.="<tr><td>Email: ".$row['email']."</td></tr>";

            $s.="<tr><td>schrieb am ".$row['datum']."</td></tr>";

            $s.="<tr><td>".$row['eintrag']."</td></tr>";

            $s.="<tr><td align='right'><a href='gastbuch.php?subject=del&nr=".$row['nummer']."'>l&ouml;schen</a></td></tr>"; 

            $s.="</table>\n";

            echo $s;

        }

    }

   

    echo("<p><a href='gastbuch.php'>Start</a></p>");

    mysqli_close($conn);

}

 

function loeschen()

{

    $nummer=$_REQUEST['nr'];

 

    $conn=inidb();

    $sql="DELETE FROM gastbuch WHERE nummer=$nummer";

    if(!mysqli_query($conn, $sql))echo mysqli_error($conn); 

    mysqli_close($conn);

    

    editieren();

}

 

function login()

{

    $s="<form method='POST' action='gastbuch.php'>";

    $s.="<input type='hidden' name='subject' value='check'>";

    $s.="<table class='smalltab'>";

    $s.="<tr><td><b>Admin Login</b></td></tr>";

    $s.="<tr><td>Passwort: <input type='text' name='pw'></td></tr>";

    $s.="<tr><td align='right'><input type='submit' value='Login'></td></tr>";

    $s.="</table></form>\n";

    echo $s;

}

 

 

function start()

    $s="<form method='POST' action='gastbuch.php'><input type='hidden' name='subject' value='save'>";

    $s.="<table class='bigtab'>";

    $s.="<tr><td>G&auml;stebuch von Mein Name</td></tr>";

    $s.="<tr><td>Name: <input type='text' name='username' size='40'></td></tr>";

    $s.="<tr><td>Email: <input type='text' name='email' size='40'></td></tr>";

    $s.="<tr><td>Eintrag: <textarea name='eintrag' rows='4' cols='72'></textarea></td></tr>";    

    $s.="<tr><td align='right'><input type='submit' value='Speichern'></td></tr>";

    $s.="</table></form>\n";

    echo $s;

 

    anzeigen();

    echo("<p><a href='gastbuch.php?subject=admin'>Admin-Login</a></p>");

}

 

 

function speichern()

{

    $username=$_REQUEST['username'];

    $email=$_REQUEST['email'];

 

    $conn=inidb();

    $eintrag=mysqli_real_escape_string($conn,$_REQUEST['eintrag']);

   

    $da=date("Y-m-d");

    $sql="INSERT INTO gastbuch SET name='$username',email='$email',eintrag='$eintrag',datum='$da'";

    if (!mysqli_query($conn, $sql))echo mysqli_error($conn); 

    mysqli_close($conn);

  

    start();

}

 

function anzeigen()

{

    global $gastbuchtitel;

 

    $conn=inidb();

    echo("G&auml;stebuch von ".$gastbuchtitel);  

  

    $result = mysqli_query($conn, "SELECT * FROM gastbuch ORDER BY nummer DESC");

    if (mysqli_num_rows($result) > 0)

    {

        while($row = mysqli_fetch_assoc($result))

        {

            $s="<table class='bigtab'>";

            $s.="<tr><td>Name: ".$row['name']."</td></tr>";

            $s.="<tr><td>Email: ".$row['email']."</td></tr>";

            $s.="<tr><td>schrieb am ".$row['datum']."</td></tr>";

            $s.="<tr><td>".$row['eintrag']."</td></tr>";  

            $s.="</table>\n";

            echo $s;

        }

    }

    mysqli_close($conn);

}

 

// ************ MySQL-Funktionen *************

function inidb()

{

    global $mysqldatenbank, $mysqlusername, $mysqlpasswort;

 

    $conn = mysqli_connect("localhost", $mysqlusername, $mysqlpasswort,$mysqldatenbank);

    if (!$conn) {

        die("Connection failed: " . mysqli_connect_error());

    }

    return $conn;

}    

    

function install()

{

    global $mysqldatenbank, $mysqlusername, $mysqlpasswort;

 

    //Verbindung zum MySQL-Server herstellen    

    $conn = mysqli_connect("localhost", $mysqlusername, $mysqlpasswort);

    if (!$conn) {

        die("Connection failed: " . mysqli_connect_error());

    }

 

    //Datenbank erstellen

    if (mysqli_query($conn, "CREATE DATABASE ".$mysqldatenbank))

    {

        echo "Database created successfully";

    } else {

        echo "Error creating database: " . mysqli_error($conn);

    }

    mysqli_close($conn);

 

    $conn = mysqli_connect("localhost", $mysqlusername, $mysqlpasswort,$mysqldatenbank);

    if (!$conn) {

        die("Connection failed: " . mysqli_connect_error());

    }

 

    

    //Tabelle erstellen

    $sql="CREATE TABLE gastbuch (nummer INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,";

    $sql.="name TEXT, datum DATE, eintrag TEXT, email TEXT)";

    if (mysqli_query($conn, $sql)) {

    echo "Table MyGuests created successfully";

    } else {

        echo "Error creating table: " . mysqli_error($conn);

    }

 

 

    echo "<p>MySQL-Installation beendet!";

    mysqli_close($conn);

}

 

 

 

?>

    </div> <!-- content -->

</div> <!-- rahmen -->

</body>

</html>