简体   繁体   English

这个查询怎么了?

[英]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): 问题是我总是遇到这个错误(通过一个没有奇怪字符的形式输入一个简单的varchar):

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 警告:mysql_fetch_array():提供的参数不是第52行/home/piscolab/public_html/keepyourlinks.com/Recetas/registro.php中的有效MySQL结果资源

  • $this->nombre has a not null value (checked) $ this-> nombre的值不为null(已选中)
  • 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 : PHP手册说

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. 资源mysql_query(字符串$ query [,resource $ link_identifier])mysql_query()向与指定link_identifier关联的服务器上的当前活动数据库发送一个唯一查询(不支持多个查询)。

If you do not have a "currently active database" then your mysql_* calls will fail. 如果您没有“当前活动的数据库”,则您的mysql_ *调用将失败。 It's always best practice to supply the MySQL connection link identifier with your calls. 最好的做法是在调用时提供MySQL连接链接标识符。

Where are you calling mysql_connect? 您在哪里调用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.. 似乎有一个不同的错误(带有属性名称,tipical ...),但仍然值得,因为发现了这些错误。

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. 而且我在您的代码中看不到任何mysql_fetch_array()语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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