This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrear.php
81 lines (54 loc) · 2.08 KB
/
crear.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
session_start();
$servername = "localhost";
$username = "tekhmos";
$password = "Tekhmos12";
$dbname = "BadDays";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection error with DBMS: " . $conn->connect_error);
}
function generarCodigoSala($conn) {
$room_code = rand(0, 9999);
$sql_check_room = "SELECT * FROM salas WHERE room_code = '$room_code'";
$result = $conn->query($sql_check_room);
if ($result->num_rows > 0) {
return generarCodigoSala($conn);
} else {
return $room_code;
}
}
if (isset($_POST['create_room'])) {
$room_code = generarCodigoSala($conn);
$_SESSION['username'] = $_POST['username'];
$sql_insert_sala = "INSERT INTO salas (room_code) VALUES ('$room_code')";
if ($conn->query($sql_insert_sala) === TRUE) {
$sql_insert_usuario = "INSERT INTO usuarios (nombre, room_code) VALUES ('" . $_SESSION['username'] . "', '$room_code')";
if ($conn->query($sql_insert_usuario) === TRUE) {
echo '<form id="redirectForm" action="./pedidos.php" method="POST">';
echo '<input type="hidden" name="room_code" value="' . $room_code . '">';
echo '<input type="hidden" name="username" value="' . urlencode($_SESSION['username']) . '">';
echo '</form>';
echo '<script>document.getElementById("redirectForm").submit();</script>';
exit;
} else {
echo "Error joining session. " . $conn->error;
}
} else {
echo "Error creating room: " . $conn->error;
}
}
$sql_create_salas_table = "CREATE TABLE IF NOT EXISTS salas (
room_code INT(6) PRIMARY KEY
)";
// Crear la tabla de usuarios en la base de datos
$sql_create_usuarios_table = "CREATE TABLE IF NOT EXISTS usuarios (
usuario_id INT AUTO_INCREMENT PRIMARY KEY,
nombre VARCHAR(255),
room_code INT(6),
FOREIGN KEY (room_code) REFERENCES salas(room_code)
)";
echo "Wrong data received.";
// Cerrar la conexión
$conn->close();
?>