Simpel gæstebog

index.php

<? require("sql.php");

switch (
$_POST["mode"]) {
case 
"add_message":
    
    if (
mysql_query("INSERT INTO messages SET message='".$_POST["message"]."', Author='".$_POST["author"]."', Date=NOW()")) {
        
$valid 1;
    } else {
        
$valid 0;
    }
    
header("Location: ".$_SERVER["PHP_SELF"]."?valid=".$valid);
    break;


default:
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>

    <head><title>G&aelig;stebog</title>
        <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-15" />
        <link rel="stylesheet" href="style.css" type="text/css" />    
        
        <style>
        input, textarea {
            width: 200px;
        }
        </style>
    </head>

    <body>
        <div id="main">
            
            <div id="content">
            <br>
            <?
            
if (isset($_GET["valid"]) && $_GET["valid"] == 1) {
                echo 
"Tak for din hilsen!";
            } elseif (isset(
$_GET["valid"]) && $_GET["valid"] == 0) {
                echo 
"Der opstod en fejl.";
            }
            
?>
            <form action="<?=$_SERVER["PHP_SELF"]?>" method="post">
        
            <label for="author">Navn</label><br/>
            <input type="text" id="author" name="author"/><br/>
            <br/>
            <label for="message">Besked</label><br/>
            <textarea id="message" name="message"></textarea><br/>
            <br/>
            <input type="submit" value="Send hilsen">
            <input type="hidden" name="mode" value="add_message">
            
            </form>
            <?
            $res_msg 
mysql_query("SELECT * FROM messages ORDER BY Date DESC");
            while (
$row_msg mysql_fetch_array($res_msg)) {
                
?>
                Besked af <i><?=$row_msg["Author"]?></i><br>
                <?=$row_msg["Message"]?><br/>
                <hr noshade>
                <?
            
}
            
?>
            <br>
            </div>
        </div>
    </body>
    </html>
    <?    
}

?>

sql.php

<?

mysql_connect
("localhost","root","");
mysql_select_db("guestbook");


?>

guestbook.sql

CREATE TABLE `messages` (
  `ID` int(11) NOT NULL auto_increment,
  `Message` text NOT NULL,
  `Author` varchar(255) NOT NULL default '',
  `Date` timestamp NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`ID`)
);