- Entrou
- Set 19, 2006
- Mensagens
- 7,829
- Gostos Recebidos
- 67
Boas pessoal aqui fica um script simples de guestbook em php e mysql.
Criar tabela mysql
Script php
Criar tabela mysql
CREATE TABLE guests (
id int(10) NOT NULL auto_increment,
name varchar(50) NOT NULL,
message varchar(255) NOT NULL,
date timestamp(14) NOT NULL,
PRIMARY KEY (id)
)
Script php
<?php
$host = "localhost";
$user = "username";
$pass = "password";
$db = "database";
//Ligar à bd
mysql_connect($host, $user, $pass) OR die ("Could not connect to the server.");
mysql_select_db($db) OR die("Could not connect to the database.");
$name = stripslashes($_POST['txtName']);
$message = stripslashes($_POST['txtMessage']);
if (!isset($_POST['txtName'])) {
$query = "SELECT id, name, message, DATE_FORMAT(date, '%D %M, %Y @ %H:%i') as newdate FROM guests ORDER BY id DESC";
$result = mysql_query($query);
//Imprimir mensagens
while ($row = mysql_fetch_object($result)) {
?>
<p><strong><?php echo $row->message; ?></strong>
<br />Enviada por <?php echo $row->name; ?> on <?php echo $row->newdate; ?></p>
<?php
}
?>
<p>Enviar Mensagem</p>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<p><label for="txtName">Nome:</label><br />
<input type="text" title="Enter your name" name="txtName" /></p>
<p><label for="txtMessage">Mensagem:</label><br />
<textarea title="Introduza a sua mensagem" name="txtMessage"></textarea></p>
<p><label title="Enviar mensagem">
<input type="submit" value="Enviar" /></label></p>
</form>
<?php
}
else {
// Adds the new entry to the database
$query = "INSERT INTO guests SET message='$message', name='$name', date=NOW()";
$result = mysql_query($query);
// Takes us back to the entries
$ref = $_SERVER['HTTP_REFERER'];
header ("Location: $ref");
}
?>