I am trying to insert data submitted in a form, into a MySQL Database, and I can’t see the problem in my code. Each time i try to insert it it ends up on a white page and the database remains empty.
this is the html form
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registro para el sorteo</title>
</head>
<body>
<h1>Registro para el sorteo</h1>
<form action="register.php" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="nombre">Nombre:</label>
<input type="text" id="nombre" name="nombre" maxlength="20" required><br><br>
<label for="apellidos">Apellidos:</label>
<input type="text" id="apellidos" name="apellidos" maxlength="30" required><br><br>
<label for="fecha_nacimiento">Fecha de nacimiento:</label>
<input type="date" id="fecha_nacimiento" name="fecha_nacimiento" required><br><br>
<label for="telefono">Teléfono:</label>
<input type="text" id="telefono" name="telefono" maxlength="9" required pattern="d{9}"><br><br>
<label for="color_preferido">Color preferido:</label>
<input type="color" id="color_preferido" name="color_preferido" required><br><br>
<label for="numero">Número para el sorteo (0-999):</label>
<input type="number" id="numero" name="numero" min="0" max="999" required><br><br>
<input type="submit" value="Registrar">
</form>
</body>
</html>
And this the php part of the registration
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "sorteo";
$conn = new mysqli($localhost, $root, $root, $sorteo);
if ($conn->connect_error) {
die("Conexión fallida: " . $conn->connect_error);
}
$email = $_POST['email'];
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$fecha_nacimiento = $_POST['fecha_nacimiento'];
$telefono = $_POST['telefono'];
$color_preferido = $_POST['color_preferido'];
$numero = $_POST['numero'];
$fecha_actual = new DateTime();
$fecha_nacimiento_dt = new DateTime($fecha_nacimiento);
$edad = $fecha_actual->diff($fecha_nacimiento_dt)->y;
if ($edad < 18) {
die("Debes ser mayor de edad para participar en el sorteo.");
}
$sql = "INSERT INTO participantes (email, nombre, apellidos, fecha_nacimiento, telefono, color_preferido, numero)
VALUES ('$email', '$nombre', '$apellidos', '$fecha_nacimiento', '$telefono', '$color_preferido', '$numero')";
if ($conn->query($sql) === TRUE) {
echo "Registro exitoso. <a href='participants.php'>Ver participantes</a>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
?>
This a list to delete, update or obtain the list of participants
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sorteo";
$conn = new mysqli($localhost, $root, $root, $sorteo);
if ($conn->connect_error) {
die("Conexión fallida: " . $conn->connect_error);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = $_POST['email'];
$sql = "DELETE FROM participantes WHERE email='$email'";
$conn->query($sql);
} elseif (isset($_POST['update'])) {
$email = $_POST['email'];
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$fecha_nacimiento = $_POST['fecha_nacimiento'];
$telefono = $_POST['telefono'];
$color_preferido = $_POST['color_preferido'];
$sql = "UPDATE participantes SET nombre='$nombre', apellidos='$apellidos', fecha_nacimiento='$fecha_nacimiento', telefono='$telefono', color_preferido='$color_preferido' WHERE email='$email'";
$conn->query($sql);
}
}
$sql = "SELECT * FROM participantes";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lista de Participantes</title>
</head>
<body>
<h1>Lista de Participantes</h1>
<table border="1">
<tr>
<th>Email</th>
<th>Nombre</th>
<th>Apellidos</th>
<th>Fecha de Nacimiento</th>
<th>Teléfono</th>
<th>Color Preferido</th>
<th>Número</th>
<th>Acciones</th>
</tr>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<form method='post' action='participants.php'>";
echo "<td><input type='hidden' name='email' value='" . $row['email'] . "'>" . $row['email'] . "</td>";
echo "<td><input type='text' name='nombre' value='" . $row['nombre'] . "'></td>";
echo "<td><input type='text' name='apellidos' value='" . $row['apellidos'] . "'></td>";
echo "<td><input type='date' name='fecha_nacimiento' value='" . $row['fecha_nacimiento'] . "'></td>";
echo "<td><input type='text' name='telefono' value='" . $row['telefono'] . "'></td>";
echo "<td><input type='color' name='color_preferido' value='" . $row['color_preferido'] . "'></td>";
echo "<td>" . $row['numero'] . "</td>";
echo "<td><button type='submit' name='update'>Actualizar</button> <button type='submit' name='delete'>Eliminar</button></td>";
echo "</form>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='8'>No hay participantes registrados.</td></tr>";
}
?>
</table>
</body>
</html>
<?php
$conn->close();
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
?>
And the sql code
CREATE DATABASE sorteo;
USE sorteo;
CREATE TABLE participantes (
email VARCHAR(50) PRIMARY KEY,
nombre VARCHAR(20) NOT NULL,
apellidos VARCHAR(30) NOT NULL,
fecha_nacimiento DATE NOT NULL,
telefono VARCHAR(9) NOT NULL,
color_preferido VARCHAR(7) NOT NULL,
numero INT NOT NULL
);
As i said i tried to make it so when the form is filled the data adds to the database on mysql
New contributor
somedudewithlizards is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.