Search code examples
phpuser-registration

What's wrong with this query?


i'm trying to define a User registration class and this is the function i have for now

 <?php

///// SE SUPONE QUE AQUI EL USUARIO YA HA INTRODUCIDO SUS DATOS DE REGISTRO


/* Conectando la Base de Datos */
include("includes/basedatos.php");

require_once("includes/funciones.php");

class registro_usuarios
{

    var $pass;
    var $email;
    var $nombre;

     public function tratandovariables()
    {

        /* Eliminando Caracteres Especiales */
        $password = htmlspecialchars($_POST['pass']);
        $mail = htmlspecialchars(strip_tags($_POST['mail']));
        $nombre = htmlspecialchars(strip_tags($_POST['nombre']));

        if (preg_match("/^[a-zA-Z0-9\-_]{3,20}$/", $nombre))
        {
            /* Asignando Valor */
            $this->pass = md5($password);
            $this->email = $mail;
            $this->nombre = $nombre;
        }
        else
        {
            echo "El nombre de usuario no es válido<br>";
            exit;
        }
    }

    public function register()
    {
        $this->tratandovariables();




        /* Comprobando si existe el usuario */
        $check = "SELECT * FROM usuarios WHERE alias = '$this->nombre'";
        $qry = mysql_query($check);

        /* La compracion */
            if (mysql_num_rows($qry))
            {
                echo "Lo sentimos, el nombre de usuario ya esta registrado.<br />";
                mysql_free_result($qry);
                return false;
            } else
            {





                $insert = "INSERT INTO usuarios (alias, pass, email, fid, fechar, ultima, img_src, reputacion) VALUES ('".$this->nombre."','".$this->pass."','".$this->email."','-1', 'NOW()', 'NOW()',' ', '0' )";
                $qry = mysql_query($insert);
                    if(mysql_affected_rows())
                    {
                        echo "El Usuario $this->nombre se Registro Correctamente";
                        return true;
                    }
                    else
                    {
                        echo "Error Ingresando datos";
                        return false;
                    }
                return false;
            }
    }

}
?>

And the problem it's that i'm allways given this error (entering a simple varchar through a form with no weird chars):

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/piscolab/public_html/keepyourlinks.com/Recetas/registro.php on line 52 El Usuario toni se Registro Correctamente

  • $this->nombre has a not null value (checked)
  • Database its empty, so there should be never results.
  • The problem it's that the script goes on and pretends that user has been registered, even shows the name! and there is not an update on database..

I just can't see the problem.. can you?

thank you!


Solution

  • Well, thank you very much for helping me,

    it seems that there was a different error (with a atribute name, tipical...) but stills worthy because found out about those errors..

    If some one needs it, the class code: (adapt your atributes)

    class registro_usuarios
    {
    
        var $pass;
        var $email;
        var $nombre;
    
         public function tratandovariables()
        {
    
            /* Eliminando Caracteres Especiales */
            $password = htmlspecialchars($_POST['pass']);
            $mail = htmlspecialchars(strip_tags($_POST['mail']));
            $nombre = htmlspecialchars(strip_tags($_POST['nombre']));
    
            if (preg_match("/^[a-zA-Z0-9\-_]{3,20}$/", $nombre))
            {
                /* Asignando Valor */
                $this->pass = md5($password);
                $this->email = $mail;
                $this->nombre = $nombre;
            }
            else
            {
                echo "El nombre de usuario no es válido<br>";
                exit;
            }
        }
    
        public function register()
        {
            $this->tratandovariables();
    
    
    
    
            /* Comprobando si existe el usuario */
            $check = "SELECT * FROM usuarios WHERE alias = '".$this->nombre."'";
            $qry = mysql_query($check);
    
            /* La compracion */
                if (mysql_num_rows($qry))
                {
                    echo "Lo sentimos, el nombre de usuario ya esta registrado.<br />";
                    mysql_free_result($qry);
                    return false;
                } else
                {
    
    
    
    
    
                    $insert = "INSERT INTO usuarios (alias, pass, mail, fid, fechar, ultima, img_src, reputacion) VALUES ('".$this->nombre."','".$this->pass."','".$this->email."','-1', NOW(), NOW(),' ', 0 )";
                    $qry = mysql_query($insert);
                        if(mysql_affected_rows())
                        {
                            echo "El Usuario $this->nombre se Registro Correctamente";
                            return true;
                        }
                        else
                        {
                            echo "Error Ingresando datos";
                            return false;
                        }
                    return false;
                }
        }
    
    }
    

    Thanks again!