Entiendo que hay preguntas similares ya respondidas pero ninguna de las soluciones que he encontrado resuelve mi problema.
Mi archivo php se ejecuta correctamente(ya que php no me muestra errores en el navegador), validando todos los campos e incluso me muestra la url que indico al final con el mensaje ?register=success
al enviar el formulario. Sin embargo, los datos no son insertados en mi base de datos.
<?php
if (isset($_POST["submit"])){
echo '<pre>Before Connect' . print_r($_POST, true) . '</pre>'; // display the $_POST array
include_once "dbh_inc.php";
$first = mysqli_real_escape_string($conn, $_POST["first"]);
$last = mysqli_real_escape_string($conn, $_POST["last"]);
$nickname = mysqli_real_escape_string($conn, $_POST["nickname"]);
$password = mysqli_real_escape_string($conn, $_POST["password"]);
$email = mysqli_real_escape_string($conn, $_POST["email"]);
//Birthdate
$birthDay = mysqli_real_escape_string($conn, $_POST["birth-day"]);
$birthMonth = mysqli_real_escape_string($conn, $_POST["birth-month"]);
$birthAge = mysqli_real_escape_string($conn, $_POST["birth-age"]);
$birthDate = $birthDay."-".$birthMonth."-".$birthAge;
$speciality = mysqli_real_escape_string($conn, $_POST["speciality"]);
$gender = mysqli_real_escape_string($conn, $_POST["gender"]);
//Error handlers
//Check for empty fields
if (empty($first) || empty($last) || empty($email) || empty($last) || empty($last) || empty($last)) {
header("Location: ../register.php?register=empty");
exit();
}else{
//Check if input characters are valid
if(!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)){
header("Location: ../register.php?register=invalid");
exit();
}else{
//Check if email is valid
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../register.php?register=email");
exit();
}else{
//Check if birthdate is valid
if (!checkdate ($birthMonth , $birthDay, $birthAge)) {
header("Location: ../register.php?register=date");
exit();
}else{
//Check if user exists
$sql = "SELECT * FROM users WHERE user_nickname='$nickname'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
//Entering to the database to check if username exists
if ($resultCheck > 0) {
header("Location: ../register.php?register=usertaked");
exit();
}else{
//Hashing the password
$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
//Insert user registration data into the database
echo "Successful insertion!!!";
$sql = "INSERT INTO users (user_first, user_last, user_nickname, user_password, user_email, user_birth_date, user_speciality, user_gender) VALUES ('$first', '$last', '$nickname', '$password', '$email', '$birthDate', '$speciality', '$gender')";
mysqli_query($conn, $sql);
header("Location: ../register.php?register=success");
exit();
}
}
}
}
}
}else{
header("Location: ../register.php");
exit();
}
Finalmente, este es el php (bdh_inc.php) en el que hago la conexión a la base de datos:
<?php
$dbServername = "localhost";//because there's running a local server
$dbUsername = "root";
$dbPassword = "";
$dbName = "login_system";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName) or die ("Conexion fallida");