Apart from the syntactic and programming errors I have detected, I have noticed that at no time you have returned the number "1" that hopes to receive the XHR petition launched from javascript. This prevents your code from working properly if the user is registered in the database.On the other hand, the second most serious error I have found has been the concatenation of strings without escaping to an SQL query. This causes your query to fail if any field has a grid inside or, worse still, can suffer https://www.youtube.com/watch?v=EpKIhF7naSY partners https://es.wikipedia.org/wiki/Inyecci%C3%B3n_SQL .To solve it we can make use of prepared queries or use http://php.net/mysqli.real-escape-string (as I have done, so I don't have to make many modifications to the code) to escape the contents of the variable previously.I have left comments by the code to explain the modifications I have made and the errors I have detected:<?php
/* Aquí olvidaste el ; del final */
include "../conexion/conexion.php";
/* Aquí olvidaste poner como primer parámetro la conexión mysqli /
mysqli_set_charset($mysqli, "utf8");
$results = 'SELECT * FROM Usuarios';
$rec = mysqli_query($mysqli, $results);
/ Si falló la consulta informamos de ello /
if ($rec === false) {
die('ERROR SQL: ' . htmlspecialchars(mysqli_error($mysqli)));
}
while ($results = mysqli_fetch_object($rec)) {
/ Hacemos una comparación en minúsculas (si quieres) /
if(mb_strtolower($results->Username) == mb_strtolower($_POST['email'])) {
/ No es necesario marcar y luego comprobar, podemos finalizar aquí mismo */
die('<span class="error">Este usuario ya ha sido registrado anteriormente.</span>');
}
}
/* Escapamos las cadenas correctamente para evitar tanto inyección SQL
como cadenas mal formadas /
$name = mysqli_real_escape_string($mysqli, $_POST['name']);
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
$telefono = mysqli_real_escape_string($mysqli, $_POST['telefono']);
$movil = mysqli_real_escape_string($mysqli, $_POST['movil']);
$nif = mysqli_real_escape_string($mysqli, $_POST['nif']);
$direccion = mysqli_real_escape_string($mysqli, $_POST['direccion']);
$postal = mysqli_real_escape_string($mysqli, $_POST['postal']);
$poblacion = mysqli_real_escape_string($mysqli, $_POST['poblacion']);
$provincia = mysqli_real_escape_string($mysqli, $_POST['provincia']);
$pass = mysqli_real_escape_string($mysqli, $_POST['pass']);
$sexo = mysqli_real_escape_string($mysqli, $_POST['sexo']);
/ Te aconsejo fechas en formato YYYY-MM-DD, localiza sólo al mostrar */
$fecha = date('y,m,d');
$results = "
INSERT INTO Usuarios (
Fecha,
Sexo,
Nombre,
Password,
Username,
Direccion,
Postal,
Poblacion,
Provincia,
Telefono,
Movil,
Dni,
intestado
) VALUES (
'$fecha',
'$sexo',
'$name',
'$pass',
'$email',
'$direccion',
'$postal',
'$poblacion',
'$provincia',
'$telefono',
'$movil',
'$nif',
'1'
)
";
if (mysqli_query($mysqli, $results) === false) {
/* Aquí olvidaste poner como primer parámetro la conexión mysqli /
die('Error SQL: ' . htmlspecialchars(mysqli_error($mysqli)));
}
/ Si todo ha ido bien tu aplicación espera encontrar el texto "1" */
echo "1";