Crea la base de datos,

Funciona el registro (sin validar si existe)
Y mostar la lista de participantes
This commit is contained in:
German Correa 2017-12-01 21:27:32 -03:00
parent 9b1590d8f1
commit 379507e346
9 changed files with 192 additions and 55 deletions

View File

@ -1,5 +1,8 @@
<?php
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include_once $root.'/db/db.php';
function verifyCaptcha(){
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
@ -64,18 +67,20 @@ $filename = "2021y22";
// validate the variables ======================================================
$data['nombre'] = $_POST['nombre'];
$data['apellido'] = $_POST['apellido'];
$data['pais'] = $_POST['pais'];
$data['ciudad'] = $_POST['ciudad'];
$data['email'] = $_POST['email'];
$data['titulo'] = $_POST['titulo'];
$data['llegada'] = $_POST['bda'];
$data['partida'] = $_POST['eda'];
$data['afiliacion'] = $_POST['afiliacion'];
$data['ciudad'] = $_POST['ciudad'];
$data['pais'] = $_POST['pais'];
$data['email'] = $_POST['email'];
$data['fechaLlegada'] = $_POST['bda'];
$data['fechaPartida'] = $_POST['eda'];
$data['financiacion'] = $_POST['financiacion'];
$data['invitado'] = $_POST['invited'];
$data['carta'] = $_POST['letterinvited'];
$data['roomtype'] = $_POST['roomtype'];
$data['roomate'] = $_POST['roomate'];
$data['cartaInvitacion'] = $_POST['letterinvited'];
$data['roomingPref'] = $_POST['roomtype'];
$data['roommate'] = $_POST['roomate'];
$data['fechaRegistro'] = date("Y-m-d H:i:s");
$fila = $data;
if (empty($_POST['nombre']))
$errors['nombre'] = 'Nombre is required.';
@ -93,6 +98,10 @@ else if(!verifyCaptcha()){
$errors['recaptcha'] = 'Error en la validación de ReCaptcha';
}
/*GUAMBIA ACAAA!!!!*/
$errors=array();
/* NO GILEAR!!!!!*/
if ( !empty($errors)) {
// if there are items in our errors array, return those errors
@ -102,7 +111,10 @@ if ( !empty($errors)) {
else {
$data['success'] = true;
//bkpregister();
$fila = $data;
//$db = new DB();
$db->insert($fila);
/*
registrar($fila);
$mail = $data["email"];
$nombre = $data["nombre"] . " " . $data["apellido"];
@ -116,7 +128,7 @@ else {
'Content-Type: ' . "text/plain; charset=UTF-8". "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($mail, 'Registration confirmation for GGDWorkshop', $msg, $headers);
*/
$data['message'] = "Registration Successfully!";
}

10
config.php Normal file
View File

@ -0,0 +1,10 @@
<?php
class Config {
/**
* path to the sqlite file
*/
const PATH_TO_SQLITE_FILE = 'db/ggdworkshop.db';
}

104
db/db.php
View File

@ -1,46 +1,70 @@
<?php
class DB extends SQLite3{
function __construct(){
$this->open('./ggdworkshop.db');
}
function schema(){
$sql =<<<EOF
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 BOOELAN NOT NULL,
roomingPref VARCHAR(50),
roommate VARCHAR(50),
fechaRegistro DATETIME NOT NULL
);
EOF;
$ret = $this->exec($sql);
if(!$ret){
echo $this->lastErrorMsg();
} else {
echo "Schema created successfully\n";
}
$this->close();
}
$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;
}
$db = new DB();
if(!$db){
echo $db->lastErrorMsg();
}else{
echo "Opened database successfully\n";
public function schema($schema_file){
$sql = file_get_contents($schema_file);
$this->pdo->exec($sql);
}
$db->schema();
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);
}
}
$db = new DB();
$db->connect();
$db->schema($schema_path);
?>

Binary file not shown.

19
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
);

28
dbconnect.php Normal file
View File

@ -0,0 +1,28 @@
<?php
/**
* SQLite connnection
*/
class SQLiteConnection {
/**
* PDO instance
* @var type
*/
private $pdo;
/**
* return in instance of the PDO object that connects to the SQLite database
* @return \PDO
*/
public function connect() {
if ($this->pdo == null) {
try {
$this->pdo = new \PDO("sqlite:" . Config::PATH_TO_SQLITE_FILE);
} catch (\PDOException $e) {
// handle the exception here
}
}
return $this->pdo;
}
}
?>

View File

@ -72,6 +72,11 @@ $().ready(function(){
formdata.find(item => item.name === "roomtype").value = $("#roomtype").select2("data")[0].text;
var parameters = $.param(formdata);
console.log(parameters);
var post = $.post('api/register.php', parameters);
post.done(function(data){
console.log(data);
});
//console.log(parameters);
},
errorElement: "em",

39
participants.php Normal file
View File

@ -0,0 +1,39 @@
<?php $root = realpath($_SERVER["DOCUMENT_ROOT"]);?>
<?php include_once 'header.php'; ?>
<?php include_once $root.'/db/db.php'; ?>
<div class="row">
<div id="content" class="col-xs-12 col-sm-8">
<div class="row">
<div class="col-xs-12">
<h1><strong>Participants</strong></h1>
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Affiliation</th>
</tr>
</thead>
<tbody>
<?php
foreach($db->getAll() as $row){
echo "<tr>";
echo "<td> ".$row['nombre']." </td>";
echo "<td> ".$row['apellido']." </td>";
echo "<td> ".$row['afiliacion']." </td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php include_once 'sidebar.php'; ?>
<?php include_once 'footer.php'; ?>

View File

@ -36,7 +36,7 @@
<div class="form-group col-xs-12 col-sm-6">
<label class="control-label " for="afiliacion">Affiliation</label>
<div class="input-container">
<input id="afiliacion" class="form-control" name="afiliciacion"
<input id="afiliacion" class="form-control" name="afiliacion"
placeholder="Which university or organization" type="text">
</div>
</div>