0

I am currently making a registration form, and I am doing it with ajax, and php with the PDO method, but when I click the register button I see that the javascript file sends me a positive response to the console, but when looking at the base I see that the data I send is not there, I have already checked and I do not know what the error is. here is the code.

code php.

equire "Conexion.php";
     sleep(1);//retrasar consulta, un segundo
     session_start();
     $datos=array(
          'nombre' => $_POST['nombre'],
          'username' => $_POST['username'],
          'direccion' => $_POST['direccion'],
          'genero' => $_POST['genero'],
          'apellido' => $_POST['apellido'],
          'telefono' => $_POST['nombre'],
          'correo' => $_POST['correo'],
          'clave' => $_POST['clave'],
          'repclave' => $_POST['repclave']
     );
     $ans=$datos['repclave']==$datos['clave'];
     if($ans){
          $queriRegistrar=$conexion->prepare("INSERT INTO `usuarios`(`NOMBRE_USUARIO`, `NOMBRE`, `APELLIDO`, `CORREO`, `CLAVE`, `DIRECCION`, `TELEFONO`,`ID_GENERO_FK`,`ID_TIPO_USUARIO_FK`) 
          VALUES (':username',':nombre','apellido',':correo',':clave',':direccion',':telefono',':generos','1'");
          $queriRegistrar->bindParam(':username',$datos['username'], PDO::PARAM_STR);
          $queriRegistrar->bindParam(':nombre',$datos['nombre'], PDO::PARAM_STR);
          $queriRegistrar->bindParam(':apellido',$datos['apellido'], PDO::PARAM_STR);
          $queriRegistrar->bindParam(':correo',$datos['correo'], PDO::PARAM_STR);
          $queriRegistrar->bindParam(':clave',$datos['clave'], PDO::PARAM_STR);
          $queriRegistrar->bindParam(':direccion',$datos['direccion'], PDO::PARAM_STR);
          $queriRegistrar->bindParam(':telefono',$datos['telefono'], PDO::PARAM_INT);
          $queriRegistrar->bindParam(':generos',$datos['genero'], PDO::PARAM_INT);
          $queriRegistrar->execute();
          if(!$queriRegistrar){
               echo json_encode(array('error' =>true));// si no encuentra los datos, devuelve un false
          }else{

               echo json_encode(array('error' => false, 'validar' => 1 ));//mandar datos al archivo js, por JSON en caso de haberlos encontrado
          }
     }else{
          echo json_encode(array('error' =>true, 'validar'=> 404));

     }

code ajax

jQuery(document).on('submit','#formregistro',function(event){
  event.preventDefault();
  jQuery.ajax({
      url:'Php/registrarse.php',
      type:'POST',
      dataType:'json',
      data:$(this).serialize(),
      beforeSend:function(){
       $('.reg').val('Validando...');
      }
    })
    .done(function(respuesta){
      console.log(respuesta);
      if(!respuesta.error){
        if(respuesta.validar==1){
          console.log("1");
          $('.reg').val('Registrarse');
          //location.href="iniciar_sesion.php";
        }
      }else{
        if(respuesta.validar==404){
          console.log("2");
          swal("hubo un error al registrarse","las claves no coinciden", "error");
          $('.reg').val('Registrarse');
        }else{
          console.log("3");
          swal("hubo un error al registrarse","revisa tus datos e intentalo de nuevo", "error");
          $('.reg').val('Registrarse');
      }
        }
        
    })
    .fail(function(resp){
      console.log(resp.responseText);
    })
    .always(function(){
      console.log("complete");
  });
});


sorry if the code is too long :(

  • Make sure PDO is set to throw exceptions and that you can see them. See [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – Phil Sep 29 '20 at 02:25
  • Your issue is that you are wrapping your parameters in quotes. Instead of `':username'`, you just want `:username` – Phil Sep 29 '20 at 02:28

0 Answers0