Results are returning true when false

Hello again. This code is returning the result of 1 when it should be returning 0 as there are no records in the table of my database. If you can point out my error I would appreciate it.
PHP Code:

//get the username
$username = mysql_real_escape_string($_POST['user_name']);

//mysql query to select field username if it's equal to the username that we check '
$result = mysql_query('SELECT user_name FROM ajax_users WHERE user_name = "'. $username .'"');
echo $username;
//if number of rows fields is bigger them 0 that means it's NOT available '
if(mysql_num_rows($result)>0){
	//and we send 0 to the ajax request
	echo 0;
}else{
	//else if it's not bigger then 0, then it's available '
	//and we send 1 to the ajax request
	echo 1;

  }

HTML FORM:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="signup">

            <ul class="form">
                <li><label for="user_name">Username</label></li>
                <li><input type='text' id='username' name="user_name"></li>
                <li><input type='button' id='check_username_availability' value='Check Availability'></li>
                <span id='username_availability_result'></span>
            </ul>

        </form>

Thanks

Is there a suitable match in the database:

$sql="SELECT user_name FROM ajax_users WHERE user_name = $username";

echo $sql;

$result = mysql_query($sql);

Run the query echoed in PHPMyAdmin, does any username match?

There is no data in the table. I have added records and tested adding them via another form and that all works fine. But now there are no records in the table. We can discount that.
Thanks

Hi Have you tried


WHERE user_name LIKE $username";

Im sure I have had this before and I got round it with this.

Hope it helps

Keith

Now I’m getting this:

SELECT user_name FROM ajax_users WHERE user_name LIKE
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\\wamp\\www\	echterms5\\index.php on line 26

It’s blank after LIKE as if $username isn’t getting populated with a value from the form and then the num_rows() error I don’t know about.
Maybe the form isn’t passing the value?

Next, try:

[COLOR=Black]

$num_rows = mysql_num_rows($result);

echo "$nu_rows were returned";

[/COLOR]

What is echoed?

You are getting that because the query failed and so mysql_query has returned false.

Echo’s this



Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in 

Have a look what is being given to the query.

Put :


print_r($_POST);
exit;



after the $username = mysql_real_escape_string($_POST[‘user_name’]);
and see what is returned to the screen that will give you a basis of what the query is using then try echoing out the sql like this

$sql = “SELECT user_name FROM ajax_users WHERE user_name LIKE ‘$username’”;
echo $sql;
exit;
$result = mysql_query($sql);

Returns:


Array ( )

try echoing out the sql

Returns

SELECT user_name FROM ajax_users WHERE user_name LIKE ''

I really appreciate your help. I’m at a loss.

That means that your form isnt passing any information to itself.

The form pseudo speaking should be like this

PHP
if trigger is true and form is submitted

Do the form processing thing we just spoke about

else

show the form for inputting and the trigger for processing the database once the form is submitted.

so when the page loads it looks for the condition to be true to process the form.

Can you put the code up?

K

If I use this code

$username = @mysql_real_escape_string($_POST['user_name']);

$sql = 'SELECT user_name FROM ajax_users WHERE user_name LIKE "'. $username .'"';

$result = mysql_query($sql);


//mysql query to select field username if it's equal to the username that we check 
echo $username;

as you put up along with my capture of $username then it returns

1

I can put whatever code you want up. What would you like the javascript file? I posted the php and the form alread.

Try this


<?php 
if(isset($_POST['form1']) && $_POST['form1']=='OK'){
$hostname = "localhost";
$database = "your database name";
$username = "your database user";
$password = "your database password";
$connection = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); 

//get the username
$username = mysql_real_escape_string($_POST['user_name']);

//mysql query to select field username if it's equal to the username that we check '
mysql_select_db($database, $connection);
$result = mysql_query('SELECT user_name FROM ajax_users WHERE user_name = "'. $username .'"',$connection);
echo $username;
//if number of rows fields is bigger them 0 that means it's NOT available '
if(mysql_num_rows($result)>0){
	//and we send 0 to the ajax request
	echo 0;
}else{
	//else if it's not bigger then 0, then it's available '
	//and we send 1 to the ajax request
	echo 1;

  }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="signup">

            <ul class="form">
                <li><label for="user_name">Username</label></li>
                <li><input type='text' id='username' name="user_name"></li>
                <li><input type='button' id='check_username_availability' value='Check Availability'></li>
                <span id='username_availability_result'></span>
            </ul>

            <input name="form1" type="hidden" id="form1" value="OK" />
</form>
</body>
</html>



Havnt tested it fully but should work

I get no connection using:

if(isset($_POST['form1']) && $_POST['form1']=='OK'){

If I remove it I get a connection and then back to my original issues.


Notice: Undefined index: user_name in C:\\wamp\\www\	echterms5\\add_user.php on line 21

and result of echo $username:


1 

This is going to turn out to be something simple I just know it.
The 1 should be 0. I’m going to sleep on it. I appreciate your help.

I think you may be right. Try checking out the submit button. Does it actually submit?

Try replacing the

<?php echo $_SERVER['PHP_SELF']; ?>

withe the name of the file ie:

thefilename.php

The $_SERVER[‘PHP_SELF’] is fine and doesn’t need to change.

The form is missing a submit element. The existing button in the form doesn’t have a type of “submit”.

Ah… yes the only bit i didnt check…the form…grrr

You need to change the line to:


<li><input type="submit" name="Submit" value="Check Availability" />

Thats the puppy!

K

That fixed the issue of passing the value from the field to the variable! I new it was something simple!
Thanks guys!