Gå til innhold

Enkel gjestebok (mysql) - Kode andre kan bruke!


Anbefalte innlegg

Hei, skal jeg vise dere en enkel gjestebok: (du kan prøve gjesteboken her: http://support-u.110mb.com/microCalendar.php)

 

 

Lagre filen som det står! - Står i parantes!

- Table -> Lag det der du lager mysql table'r

 

 

- Det er bare å sette igang:

 

 

Mysql: (table)

 

CREATE TABLE `guestbook` (

`id` int(11) NOT NULL auto_increment,

`name` varchar(100) default NULL,

`text` text,

`insertdate` datetime default NULL,

`location` varchar(100) default NULL,

`web` varchar(100) default NULL,

`email` varchar(100) default NULL,

PRIMARY KEY (`id`)

);

 

 

Koble til Mysql: (databaser/gjestebok/db.php)

 

<?php

require_once('dbXX.php');

 

$serverhost = "host";

$serveruser = "bruker";

$serverpwd = "pass";

$dbname = "databasenavn";

 

$MyDb = new cMysqlDB($serverhost,$serveruser,$serverpwd,$dbname);

?>

 

 

Koble til Mysql: (databaser/gjestebok/dbXX.php)

 

<?php

 

class cMysqlDB

{

var $connection_id;

var $result;

var $record = array();

 

function cMysqlDB($hostname, $username, $userpassword, $database, $persistent = true)

{

$this->host = $hostname;

$this->user = $username;

$this->password = $userpassword;

$this->dbname = $database;

$this->persistent = $persistent;

 

$this->connection_id = ($this->persistent) ? mysql_pconnect($this->host, $this->user, $this->password) : mysql_connect($this->host, $this->user, $this->password);

 

if ($this->connection_id)

{

if ($this->dbname != "")

{

$dbselect = mysql_select_db($this->dbname);

 

if( !$dbselect )

{

mysql_close($this->db_connect_id);

$this->connection_id = false;

}

}

return $this->connection_id;

}

else

return false;

}

 

function f_CloseConnection()

{

if( $this->connection_id )

return mysql_close($this->connection_id);

else

return false;

}

 

function f_ExecuteSql($sql = "")

{

unset($this->result);

 

if ($sql != "")

$this->result = mysql_query($sql, $this->connection_id);

 

if (!$this->result) {

$err = mysql_error();

}

 

if ($this->result)

{

unset($this->record[$this->result]);

return $this->result;

}

}

 

function f_GetSelectedRows($query_id = 0)

{

if( !$query_id ) $query_id = $this->result;

 

return ( $query_id ) ? mysql_num_rows($query_id) : false;

}

 

function f_GetAffectedRows()

{

return ( $this->connection_id ) ? mysql_affected_rows($this->connection_id) : false;

}

 

function f_GetRecord($query_id = 0)

{

if( !$query_id ) $query_id = $this->result;

if ($query_id)

{

$this->record = mysql_fetch_assoc($query_id);

return $this->record;

}

else

return false;

}

 

function f_SetRecordPointer($recordnumber, $query_id = 0)

{

if( !$query_id ) $query_id = $this->result;

 

return ( $query_id ) ? mysql_data_seek($query_id, $recordnumber) : false;

}

 

function f_GetNextId()

{

return ( $this->connection_id ) ? mysql_insert_id($this->connection_id) : false;

}

 

function f_FreeResult($query_id = 0)

{

if( !$query_id ) $query_id = $this->query_result;

 

if ( $query_id )

{

unset($this->record[$query_id]);

 

mysql_free_result($query_id);

 

return true;

}

else

return false;

}

 

function f_GetSqlError()

{

$result['message'] = mysql_error($this->connection_id);

$result['code'] = mysql_errno($this->connection_id);

 

return $result;

}

}

?>

 

 

Gjesteboken: (/index.php)

 

<style type="text/css">

body

{

background: #293442;

background-attachment: fixed;

color: #a6b6c9;

font-family: Tahoma, Tahoma, Arial, "Trebuchet MS", Sans-Serif, Georgia, Courier, "Times New Roman", Serif;

font-size: 11px;

line-height: 135%;

margin-left: 10px;

padding: 0px;

}

.input-checkbox,

input,

textarea,

select

{

background: #425963;

border: 1px solid #242f39;

color: #8aa8aa;

font-family: verdana, helvetica, sans-serif;

font-size: 11px;

margin: 5px;

padding: 2px;

vertical-align: middle;

}

</style>

<?php

require_once('databaser/gjestebok/db.php');

 

if (isset($_POST['submitBtn'])) {

$name = (isset($_POST['name'])) ? htmlentities($_POST['name']) : '' ;

$comment = (isset($_POST['comment'])) ? htmlentities($_POST['comment']) : '' ;

$location = (isset($_POST['location'])) ? htmlentities($_POST['location']) : '' ;

$website = (isset($_POST['website'])) ? htmlentities(str_replace('http://','',$_POST['website'])) : '' ;

$email = (isset($_POST['email'])) ? htmlentities($_POST['email']) : '' ;

$actDate = date("Y-m-d H:i:s");

 

//Minimum name and comment length.

if ((strlen($name) > 2) && (strlen($comment) > 5)){

$sql = "INSERT INTO guestbook (name,text,insertdate,location,web,email) VALUES (";

$sql .= "'".$name."','".$comment."','".$actDate."','".$location."','".$website."','".$email."')";

$MyDb->f_ExecuteSql($sql);

}

 

echo("Du har nå lagt til ny hilsen..");

}

else {

 

?>

<b><font color="#FF0000">* Du må fylle inn minst navn og meldingsfeltet<br>* Du må minst skrive 2 tegn på navn, og minst 3 tegn på beskjed!</font></b><br>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="gbook" id="gbook">

Navn:<br>

<input name="name" type="text" size="42" maxlength="15" /><br>

Beskjed:<br>

<textarea name="comment" cols=32 rows=6></textarea><br>

Webside:<br>

<input name="website" type="text" size="42" /><br>

Email:<br>

<input name="email" type="text" size="42" /><br>

<input class="text" type="submit" name="submitBtn" value=" Fortsett " /></td></tr>

</form>

<?php } ?>

<br>

<br>

<br>

<?php

require_once('databaser/gjestebok/db.php');

 

$sql = "SELECT * FROM guestbook ORDER BY insertdate DESC";

$result = $MyDb->f_ExecuteSql($sql);

$recordcount = $MyDb->f_GetSelectedRows();

?>

 

<!-- Svar -->

<?php while ($row = $MyDb->f_GetRecord($result)) { ?>

<div style="background-color:#999999; width:300px;"><?php echo $row['name']; ?></div><br>

<div style="border:thin dotted #000000; width:300px; height:auto"><?php echo nl2br($row['text']); ?></div><br>

<?php echo $row['insertdate']; ?>

<?php } ?>

 

 

 

 

 

Der har du hele gjesteboken :dribble:

Veldig enkel og grei!

 

Eller, hva synes dere ???

Endret av php_user
Lenke til kommentar
Videoannonse
Annonse

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...