How to avoid duplicate records in the Database?

0

I'm doing a register in PHP, and I want to do that if a user is already registered, I can not do it again. In the controller I have the following code: '

                try {
                     $daoaccesori = new DAOAccesori();
                        $rdo = $daoaccesori->comprobar_usuario($_POST);


                }catch (Exception $e){
                        echo json_encode("error");
                        exit;
                    }
                    if(!$rdo){
                        echo json_encode("error");
                        exit;

                    if(mysql_num_rows($rdo)>0){
                      echo '<script language="javascript">alert("Este correo electronico ya existe");</script>'; 
                       $callback = 'index.php?page=controller_home&op=producte';
                       die('<script>window.location.href="'.$callback .'";</script>');
                      // exit;

                   }else try{
                        $daoaccesori = new DAOAccesori();
                        $rdo = $daoaccesori->nuevo_usuario($_POST);
                    }
                    catch (Exception $e){
                        echo json_encode("error");
                        exit;
                    }
                    if(!$rdo){
                        echo json_encode("error");
                        exit;
                    }else{
                                 echo '<script language="javascript">alert("Se ha registrado correctamente");</script>'; 
                                 $callback = 'index.php?page=controller_home&op=producte';
                                 die('<script>window.location.href="'.$callback .'";</script>');

                    }
                }'

And the problem is that now it does not register or leave as you already are. The result that shows $ rdo is as follows: mysqli_result Object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 2 [type] => 0 ) But then, it does not do what I want ... Any suggestions? Thanks

    
asked by Carloscr99 05.02.2018 в 12:24
source

2 answers

0

Finally I managed to solve the problem, this was that in my SQL statement, I returned a 1 or a 0, finally, the SQL query to the birthday has stayed like this:

function login_usuario($correo, $contrasenya){


        $sql = "SELECT * FROM usuarios WHERE correo='$correo' and contrasenya='$contrasenya'";


        $conexion = connect::con();
        $res = mysqli_query($conexion, $sql)->fetch_object();
        connect::close($conexion);
        return $res;
        }

And the login controller stays like this:

if ($_POST){

          $correo=$_POST['email'];
          $contrasenya=$_POST['contrasenya'];

                try {
                     $daoaccesori = new DAOAccesori();
                    $rdo = $daoaccesori->login_usuario($correo,$contrasenya);


                }catch (Exception $e){
                        echo json_encode("error1");
                        exit;
                    }

                 if($rdo){
                     $_SESSION['user']=$rdo;
                  /*   print_r("que te _SESSION");
                  print_r ($_SESSION['user']=$rdo);
                 // die();*/
                       echo '<script language="javascript">alert("Bienvenido!");</script>'; 
                       $callback = 'index.php?page=controller_home&op=producte';
                       die('<script>window.location.href="'.$callback .'";</script>');



                }
                else {

                        echo '<script language="javascript">alert("Email o Contraseña incorrectos");</script>'; 
                        $callback = 'index.php?page=controller_login&op=login';
                        die('<script>window.location.href="'.$callback .'";</script>');

                 }

      }

                     include("module/login/view/login.php");
        break;

Now, if it works $ _SESSION ['user'] = $ rdo, so I can show different menus, depending on the user that is registered, among other things.

    
answered by 10.02.2018 / 11:36
source
0

You have gone through the num_rows to know what result returns you. Have you tried to perform If you are registered or not, that you return one answer or another, I ask?

    
answered by 05.02.2018 в 12:27