A
asder99
Guestbook in php. Qualche consiglio?
Codice:
<?php
class Guestbook {
public $con;
public $titolo;
public $nick;
public $testo;
// Costruttore
public function __construct($tit, $nic, $tes) {
$this -> con = @new mysqli("localhost", "root", "", "sito");
if(mysqli_connect_errno() != 0) {
print "<td width='700'>Connection failed (" . mysqli_connect_errno() . "): " . mysqli_connect_error() . "</td>";
}
$this -> titolo = $tit;
$this -> nick = $nic;
$this -> testo = $tes;
$this -> control_all($titolo, $nick, $testo);
$this -> control_tit($titolo);
$this -> control_nic($nick);
$this -> control_tes($testo);
$this -> con -> query("SET NAMES 'uft8'");
$this -> insert($titolo, $nick, $testo);
$this -> view();
}
// Controllo che non ci siano variabili vuote
public function control_all($titolo, $nick, $testo) {
if(!$this->titolo or !$this->nick or !$this->testo) {
print "<td>Input incompleto</td>" and die;
}
}
// Controllo che le varibili siano solo lettere e numeri
public function control_tit($titolo) {
if(!preg_match("/^[a-z]{3,20}$/", $this -> titolo)) {
print "<td>Input errato</td>" and die;
}
}
// Controllo che le varibili siano solo lettere e numeri
public function control_nic($nick) {
if(!preg_match("/^[a-z]{3,10}$/", $this -> nick)) {
print "<td>Input errato</td>" and die;
}
}
// Controllo che le varibili siano solo lettere e numeri
public function control_tes($testo) {
if(!preg_match("/^[a-z]{3,500}$/", $this -> testo)) {
print "<td>Input errato</td>" and die;
}
}
// Inserisco i dati nel database
public function insert($titolo, $nick, $testo) {
$query = "INSERT INTO guest(titolo, nick, testo) VALUES('$this->titolo','$this->nick','$this->testo')";
$result = @$this -> con -> query($query);
if($result === FALSE) {
print "<td>Query Fallita</td>" and die;
}
}
// Mostro i dati
public function view() {
$query = "SELECT * FROM guest ORDER BY id DESC";
$result = @$this -> con -> query($query);
if($result === FALSE) {
print "<td>Query Fallita</td>" and die;
}
while(($row = @$result -> fetch_assoc()) !== NULL) {
print "<tr>{$row['titolo']} {$row['nick']} {$row['testo']}</tr>";
}
}
}
?>