· 9 years ago · Jan 18, 2017, 03:48 AM
1<?php
2class GuestBook {
3
4 private $base = '';
5 private $pass = false;
6 private $db = '';
7
8 private $connected = false;
9
10 public function __construct( $database='', $basepass=false ){
11 if( !file_exists($database) )
12 {
13 throw new Exception("Can not find database file.");
14 }
15
16 $this->base = $database;
17
18 if ($this->db = new SQLite3($this->base))
19 {
20 $this->connected = true;
21 }
22 else
23 {
24 throw new Exception('Can not connect to database! Database error: ');
25 }
26 }
27
28 public function install()
29 {
30 if( $this->connected )
31 {
32 $this->db->exec("
33 CREATE TABLE if not exists Messages (
34 Messagesid INTEGER PRIMARY KEY AUTOINCREMENT,
35 Name TEXT NOT NULL ,
36 Mail TEXT NOT NULL ,
37 Mark INTEGER NOT NULL ,
38 Message TEXT NOT NULL ,
39 IP TEXT
40 );");
41 }
42 else
43 {
44 throw new Exception('Not connected to database.');
45 }
46 }
47
48 public function addNewMark($name='', $mail='', $mark=0, $message='', $ip='0.0.0.0'){
49 if( !$this->connected )
50 {
51 throw new Exception('Not connected to database.');
52 }
53 $this->db->exec("
54 INSERT INTO Messages (Name,Mail,Mark,Message,IP)
55 VALUES ('$name','$mail',$mark,'$message','$ip');
56 ");
57 }
58
59 public function getMessages( $count=10){
60 if( !$this->connected )
61 {
62 throw new Exception('Not connected to database.');
63 }
64 $q = $this->db->query("SELECT * FROM Messages LIMIT '$count'");
65 return $q->fetchArray();
66 }
67
68 public function delete( $id ) {
69 if( !$this->connected )
70 {
71 throw new Exception('Not connected to database.');
72 }
73 $this->db->exec("DELETE FROM Messages WHERE MessagesID=$id");
74 }
75}
76?>