开发者

problem in a validation form - php

i have a problem in my code. I have an ajax validation that calls a php file (where the data is validated).

The php returns echos like "invalidData" and in the javascript i check if (data=="invalidData") {//something}

The problem are the includes. Incredible thing.

 <?php
    include("includes/f_banco.php");
    conecta ();

    function get_post_var($var) {
        $val = $_POST[$var];
        if (get_magic_quotes_gpc())
        $val = stripslashes($val);
        return $val;
    }


    $name = get_post_var('name');


    function validateName($name){
        if(strlen($name) < 4 || (empty($name))) {
            echo "invalidData";
            return false;
        }
        else {

            $name = mysql_real_escape_string($name);
            $check = mysql_query("SELECT username FROM users WHERE username ='".$name."'")
            or die(mysql_error());
            $check2 = mysql_num_rows($check);

            if ($check2 == 0 && $name != "") {
                echo "validData";
                return true;
            } else {
                echo "invalidDa开发者_StackOverflowta";
                return false;
            }
        }
    }

    error_reporting(E_ALL);
    validateName($name);

    ?>

in the code above i only can check if the name is empty if i don't put the includes in the file. If i put the result is again and again different than invalidData.

The connection to the database is not made too or if is made the return is not the correct. Important: the include file is correct, i test in another example and the database is correct too.

thanks

Edit: **LAST VERSION**

<?php 
error_reporting(-1); 

require 'includes/f_banco1.php';

$name = $_POST["carlos"];


function validateName($name){
    if(strlen($name) < 4 || (empty($name))) {
        echo "nomeInvalido";
        return false;
    }
    else {

        $name = mysql_real_escape_string($name);

        $check = mysql_query("SELECT username FROM users WHERE username ='".$name."'")
        or die(mysql_error());
        $check2 = mysql_num_rows($check);

        if ($check2 == 0 && $name != "") {
            echo "nomeValido";
            return true;
        } else {
            echo "nomeInvalido";
            return false;
        }
    }
}


validateName($name);

echo "this must appear";
?>

output:

Notice: Undefined index: carlos in C:\Users\fel\VertrigoServ\www\login\validation.php on line 8
nomeInvalidothis must appear


Probably PHP debug 101... just do a

<?php
error_reporting(-1);
...
?>

And inspect the error...


Try updating your query to be the following:

SELECT `username` FROM users WHERE `username` ='".$name."'

And, another question... If you already know what the username is, then why are you running a query to find the username? I assume you're just checking to see if the username exists.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜