简体   繁体   中英

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!

删除'now()'周围的引号,否则您将作为字符串而不是MySQL时间戳插入

use concatenation here :

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

The PHP manual says :

resource mysql_query ( string $query [, resource $link_identifier ] ) mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

If you do not have a "currently active database" then your mysql_* calls will fail. It's always best practice to supply the MySQL connection link identifier with your calls.

Where are you calling mysql_connect?

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!

Try this one instead;

$check = "SELECT * FROM usuarios WHERE alias = '".$this->nombre."'";

And I don't see any mysql_fetch_array() statement in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM