Problem with Exception in PHP using POO

0

I have the following problem, which only Exemption 3 runs without going through 1 or 2.

The problem lies when the user is repeated, must show Exception 2, but this goes straight to the 3.

Next I leave the code:

From regController

<?php
    if (!isset($_SESSION['id'],$_SESSION['user'],$_SESSION['email'])) {
        if ($_POST) {
            include('core/models/class.Acceso.php');
            $acceso = new Acceso();
            $acceso->Registrar();
            exit;
        }else{
            $template = new Smarty();
            $template->display('public/registro.tpl');
        }
    }else{
        header('location: ?view=index');
    }
?>

Class Access where the Retain () function is located

public function Registrar(){
    try{
        if (!empty($_POST['user']) and !empty($_POST['pass']) and !empty($_POST['email'])) {
            $db = new Conexion();
            $this->user = $db->real_escape_string($_POST['user']);
            $this->email = $db->real_escape_string($_POST['email']);

            $sql = $db->query("SELECT * FROM user WHERE user='$this->user' OR email='$this->email'; ");
            if (false) {

            }else{
                $datos = $db->recorrer($sql);

                if (strtolower($this->user) == strtolower($_POST['user']) ) {
                // var_dump($datos);
                    throw new Exception(2);                     
                }else{
                    throw new Exception(3);

                }
            }

        }else{
            throw new Exception("Error : Campos Vacios");   
        }


    }catch(Exception $reg){
        echo $reg->getMessage();
    }


}//function Registrar

The script from reistro.tpl

 <script>
    window.onload = function(){ 
      document.getElementById('send_request').onclick = function(){

        var connect, user, pass, email, form, result;
        user = document.getElementById('user').value;
        pass = document.getElementById('pass').value;
        email = document.getElementById('email').value;

        if (user != '' && pass != '' && email != '') {
          form ='user=' + user + '&pass=' + pass + '&email=' + email;

          connect = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
          connect.onreadystatechange = function(){
            if (connect.readyState == 4 && connect.status == 200) {
                // console.log(connect.responseText);
              if (parseInt(connect.responseText) == 1) {
                result = '<div class="alert alert-dismissible alert-success" style="width: 500px;"> ';
                result += '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
                result += '<strong>Registro Completado></strong> Bienvenidos, solo un poco mas ';
                result += '</div>';
                location.href = '?view=index';
                document.getElementById('_AJAX_').innerHTML = result;
              }else if (parseInt(connect.responseText) == 2) {
                result = '<div class="alert alert-dismissible alert-success" style="width: 500px;"> ';
                result += '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
                result += '<strong>ERROR: </strong>El usuario ya existe ';
                result += '</div>'; 
                document.getElementById('_AJAX_').innerHTML = result;
              }else {
                result = '<div class="alert alert-dismissible alert-success" style="width: 500px;"> ';
                result += '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
                result += '<strong>ERROR: </strong>El Email ya existe ';
                result += '</div>'; 
                document.getElementById('_AJAX_').innerHTML = result;
              }
            }else if(connect.readyState != 4){
              result = '<div class="alert alert-dismissible alert-warning" style="width: 500px;"> ';
                result += '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
                result += 'Procesando... ';
                result += '</div>';
                document.getElementById('_AJAX_').innerHTML = result;
            }
          }
          connect.open('POST','?view=reg',true);
          connect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
          connect.send(form);
        }else{
          result = '<div class="alert alert-dismissible alert-warning" style="width: 500px;"> ';
          result += '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
          result += '<strongERROR></strong> Todos los Campos Deben estar llenos ';
          result += '</div>';
          document.getElementById('_AJAX_').innerHTML = result;
        }
      }
    }
  </script>

Any suggestion, idea or solution is welcome.

    
asked by Gamez 12.04.2017 в 21:49
source

2 answers

1

Change

(strtolower($this->user) == strtolower($_POST['user'])) 

for

(strtolower($this->user) == strtolower($datos['user']))
    
answered by 13.04.2017 / 02:42
source
0

* Execute var_dump ($ data); and I get this *

array(10) {
  [0]=>
  string(1) "1"
  ["id"]=>
  string(1) "1"
  [1]=>
  string(4) "luis"
  ["user"]=>
  string(4) "luis"
  [2]=>
  string(32) "c33367701511b4f6020ec61ded352059"
  ["pass"]=>
  string(32) "c33367701511b4f6020ec61ded352059"
  [3]=>
  string(19) "[email protected]"
  ["email"]=>
  string(19) "[email protected]"
  [4]=>
  string(1) "0"
  ["admin"]=>
  string(1) "0"
}
2
    
answered by 13.04.2017 в 00:59