adds database

This commit is contained in:
sabelo 2024-07-28 18:23:30 -03:00
parent 448956888d
commit f4a3010c3f
3 changed files with 99 additions and 0 deletions

80
db/db/db.php Normal file
View File

@ -0,0 +1,80 @@
<?php
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
$db_path = $root."/db/ggdworkshop.db";
$schema_path = $root."/db/schema.sql";
class DB{
private $pdo;
public function connect(){
if($this->pdo == null){
global $db_path;
$this->pdo = new \PDO('sqlite:'.$db_path);
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
return $this->pdo;
}
public function schema($schema_file){
$sql = file_get_contents($schema_file);
$this->pdo->exec($sql);
}
public function insert($registro){
$columns = implode(",", array_keys($registro));
echo $columns;
$sql = "INSERT INTO Registro ($columns)"
.' VALUES (:nombre,:apellido,:titulo,:afiliacion,:ciudad,:pais,:email,:fechaLlegada,'
.':fechaPartida,:financiacion,:invitado,:cartaInvitacion,:roomingPref,:roommate,:fechaRegistro);';
echo $sql;
$stmt = $this->pdo->prepare($sql);
$stmt->execute([
':nombre' => $registro['nombre'],
':apellido' => $registro['apellido'],
':titulo' => $registro['titulo'],
':afiliacion' => $registro['afiliacion'],
':ciudad' => $registro['ciudad'],
':pais' => $registro['pais'],
':email' => $registro['email'],
':fechaLlegada' => $registro['fechaLlegada'],
':fechaPartida' => $registro['fechaPartida'],
':financiacion' => $registro['financiacion'],
':invitado' => $registro['invitado'],
':cartaInvitacion' => $registro['cartaInvitacion'],
':roomingPref' => $registro['roomingPref'],
':roommate' => $registro['roommate'],
':fechaRegistro' => $registro['fechaRegistro']
]);
return $this->pdo->lastInsertId();
}
public function getAll(){
$stmt = $this->pdo->prepare("SELECT * FROM Registro");
$stmt -> execute();
return $stmt->fetchAll(\PDO::FETCH_BOTH);
}
public function findByMail($email){
$stmt = $this->pdo->prepare("SELECT * FROM Registro WHERE email=:email");
$stmt->bindParam(":email",$email);
$stmt->execute();
$result = $stmt->fetch(\PDO::FETCH_ASSOC);
if($result)
return false;
else
true;
}
}
$db = new DB();
$db->connect();
$db->schema($schema_path);
?>

BIN
db/db/ggdworkshop.db Normal file

Binary file not shown.

19
db/db/schema.sql Normal file
View File

@ -0,0 +1,19 @@
CREATE TABLE IF NOT EXISTS Registro
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
nombre VARCHAR(50) NOT NULL,
apellido VARCHAR(50) NOT NULL,
titulo VARCHAR(50),
afiliacion VARCHAR(50),
ciudad VARCHAR(50),
pais VARCHAR(50),
email VARCHAR(80) NOT NULL UNIQUE,
fechaLlegada DATE,
fechaPartida DATE,
financiacion BOOLEAN NOT NULL,
invitado BOOLEAN NOT NULL,
cartaInvitacion BOOLEAN NOT NULL,
roomingPref VARCHAR(50),
roommate VARCHAR(50),
fechaRegistro DATETIME NOT NULL
);