Jump to content

Checking a username


yogibear

Recommended Posts

Hi I have been trying to get a check on a username field so that the username is unique
this is what i have but im getting errors
[code]<?php
$con = mysql_connect('localhost','web22-james','***');
if (!$con)

$username = $_POST['username'];
$email = $_POST['email'];
$pword1 = $_POST['pword1'];
$pword2 = $_POST['pword2'];

$query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1";    // check if username already 
    $result = mysql_query($query);
if(0 != mysql_num_rows($result))
        echo "Our database shows this username has already been registered. Please choose another username.";
    } else {
            echo "WORKING!";
    }     
    mysql_close();
?> [/code]

any ideas what is the problem

many thanks

yogi
Link to comment
Share on other sites

You need to bracket that first 'if', for starters:
[code]<?php
$con = mysql_connect('localhost','web22-james','stash');
if (!$con) {
  die("Connection failed.");
}else{

$username = $_POST['username'];
$email = $_POST['email'];
$pword1 = $_POST['pword1'];
$pword2 = $_POST['pword2'];

$query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1";    // check if username already 
    $result = mysql_query($query);
if(0 != mysql_num_rows($result))
        echo "Our database shows this username has already been registered. Please choose another username.";
    } else {
            echo "WORKING!";
    }   
    mysql_close();
}
?> [/code]

What kind of errors are you getting? Post them, we'll help you sort them out.
Link to comment
Share on other sites

You're missing a { here:

[code]if(0 != mysql_num_rows($result)) [b]{[/b][/code]

And that's assuming you adapted c4onastick's changes also as that open 'if' statement will produce another error.

If statements always have code to run and that code is between curly's:

if ($this_does_not_work == true)
{
  echo "This does not work";
}
Link to comment
Share on other sites

hi im getting a error on line 14 now
[code]
<?php
$con = mysql_connect('localhost','***','***');
if (!$con) {
  die("Connection failed.");
}else{

$username = $_POST['username'];
$email = $_POST['email'];
$pword1 = $_POST['pword1'];
$pword2 = $_POST['pword2'];

$query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1";    // check if username already 
    $result = mysql_query($query);
if(0 != mysql_num_rows($result)) [b]{[/b]
        echo "Our database shows this username has already been registered. Please choose another username.";
    } else {
            echo "WORKING!";
    }   
    mysql_close();
}
?>
[/code]
Link to comment
Share on other sites

[quote author=jesirose link=topic=123684.msg511547#msg511547 date=1169576103]
This is why you don't just copy and paste code without understanding it. [b] isn't php, that's a bold tag.
Take that stuff out, and in the future POST the error, not just that you got one.
[/quote]

hi i did post the error it was in my second post and i also tried it with and without the bold tag i didnt just Copy and Paste but that is why people get help because they dont understand

thanks for help
Link to comment
Share on other sites

[quote author=yogibear link=topic=123684.msg511554#msg511554 date=1169576651]
[quote author=jesirose link=topic=123684.msg511547#msg511547 date=1169576103]
This is why you don't just copy and paste code without understanding it. [b] isn't php, that's a bold tag.
Take that stuff out, and in the future POST the error, not just that you got one.
[/quote]

hi i did post the error it was in my second post and i also tried it with and without the bold tag i didnt just Copy and Paste but that is why people get help because they dont understand

thanks for help
[/quote]

Posting the error would be like the error you posted before ERRORNAME on LINE ### on FILENAME
Link to comment
Share on other sites

This is the code
[code]<?php
$con = mysql_connect('localhost','***','***');
if (!$con) {
   die("Connection failed.");
}else{

$username = $_POST['username'];
$email = $_POST['email'];
$pword1 = $_POST['pword1'];
$pword2 = $_POST['pword2'];

$query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1";    // check if username already 
    $result = mysql_query($query);
if(0 != mysql_num_rows($result)) {
        echo "Our database shows this username has already been registered. Please choose another username.";
    } else {
            echo "WORKING!";
    }     
    mysql_close();
}
?> [/code]

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sites/***/public_html/registerconfirm.php on line 14

Many thanks

yogi
Link to comment
Share on other sites

I was gonna say, personally i've not seen that 'if' statement written in that format. Here's what I use;

$num_rows = mysql_num_rows($result);

if ($num_rows => 0) {
    echo "Our database shows this username has already been registered. Please choose another username.";
} else {
    echo "WORKING!";
}

Also, your mysql connection is missing one vital ingredient. The database name:

mysql_select_db("database_name");
Link to comment
Share on other sites

The error you are receiving indicates that the previous mysql_query did not work correctly, change this line:
[code]<?php
$result = mysql_query($query);
?>[/code]
to
[code]<?php
$result = mysql_query($query) or die("Problem with the query:<pre>$query</pre><br>" . mysql_error());
?>[/code]
The output should give you a hint as to why the query didn't work correctly. If not post the output here.

Ken
Link to comment
Share on other sites

Hi this is my code i have no idea about it i have changed removed and added bits and now im lost with it. [code]<?php
$con = mysql_connect('localhost','***','***');
mysql_select_db("web22-james");

if (!$con) {
  die("Connection failed.");
}else{

$username = $_POST['username'];
$email = $_POST['email'];
$pword1 = $_POST['pword1'];
$pword2 = $_POST['pword2'];

$query = "SELECT * FROM usersinformation WHERE username='$username' LIMIT 1";

$result = mysql_query($query) or die("Problem with the query:<pre>$query</pre><br>" . mysql_error());

$num_rows = mysql_num_rows($result);
if ($num_rows => 0) {
    echo "Our database shows this username has already been registered. Please choose another username.";
} else {
    echo "$results";
}
    mysql_close();
?>[/code]

this is the html form
[code]<form action="registerconfirm.php" method="POST">
Username: <input type="text" name="username" maxlength="16" /><br>
E-mail: <input type="text" name="email"  /><br>
Password: <input type="text" name="pword1"><br>
Password (again): <input type="text" name="pword2"><br>
<input type="submit" value="Register">
</form>[/code]

when i click submit it says Parse error: syntax error, unexpected $end in /home/sites/***/public_html/registerconfirm.php on line 25
Link to comment
Share on other sites

[quote author=yogibear link=topic=123684.msg511611#msg511611 date=1169579981]
[quote author=jesirose link=topic=123684.msg511609#msg511609 date=1169579762]
if ($num_rows => 0) {

=> is syntax for assigning array values. You want >=
[/quote]

I'm still getting the same error

thanks

yogi
[/quote]

Actually, I think you'd just want > - you don't want it to go if it's 0, just if it's > 0
Link to comment
Share on other sites

[quote author=jesirose link=topic=123684.msg511617#msg511617 date=1169580331]
[quote author=yogibear link=topic=123684.msg511611#msg511611 date=1169579981]
[quote author=jesirose link=topic=123684.msg511609#msg511609 date=1169579762]
if ($num_rows => 0) {

=> is syntax for assigning array values. You want >=
[/quote]

I'm still getting the same error

thanks

yogi
[/quote]

Actually, I think you'd just want > - you don't want it to go if it's 0, just if it's > 0
[/quote]

IT WORKS THANK YOU ;D simcoweb got it going but it always said it was in the database. jesirose fixed the last problem many thanks everyone
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.