Jump to content

need help with my small code of user name


Alidad

Recommended Posts

Hi, I'm creating small form for sign up, one of the text field called "username", I would like to have check available user name from database, if that user name are taken then send message right away after submit it. If user name avalaible then go on to next process.

 

I wrote small php code for some reason is not working right. I would appreication if you can help me to see what did i missed or correct it please.

 

My database table name member.

The field in database called username.

The text filed in the form called username.

 

 

//

 

<? php

Open conection to the database

mysql_connect('localhost', 'root', '3250');

mysql_select_db('member');

 

 

// Function to check if a username exists inside the database

function check_user_exist($username) {

$username = mysql_escape_string($username);

echo "user name is not available";

}

// Username is available

return array('yes');

}

 

?>

 

please help me thanks.

 

AM

Link to comment
Share on other sites

<?php

mysql_connect("localhost", "****", "****");
mysql_select_db("member");

function check_user_exist($username) {
$username = mysql_real_escape_string($username);
$query = mysql_query("SELECT * FROM userstable WHERE username = '{$username}'") or die(mysql_error());
$rows = mysql_num_rows($query);

if($rows > 0) {
  return false;
} else {
  return true;
}
}

if(check_user_exist($_POST["username"])) {
die("A user already exists with that username");
} else {
// Go ahead with the form processing
}

?>

 

Remove your password quick!

Link to comment
Share on other sites

He already posted it:

 

function check_user_exist($username) {
   $username = mysql_escape_string($username);
   echo "user name is not available";
   }
   // Username is available
   return array('yes');
}

Link to comment
Share on other sites

Hi, I'm creating small form for sign up, one of the text field called "username", I would like to have check available user name from database, if that user name are taken then send message right away after submit it. If user name avalaible then go on to next process.

 

I wrote small php code for some reason is not working right. I would appreication if you can help me to see what did i missed or correct it please.

 

My database table name member.

The field in database called username.

The text filed in the form called username.

 

 

//

 

<? php

Open conection to the database

mysql_connect('localhost', 'username', 'passwod');

mysql_select_db('member');

 

 

// Function to check if a username exists inside the database

function check_user_exist($username) {

$username = mysql_escape_string($username);

echo "user name is not available";

}

// Username is available

return array('yes');

}

 

?>

 

please help me thanks.

 

AM

Link to comment
Share on other sites

Thank you for your advaice to remove that user name and password, gees how stupid I am.

 

And also thank you for your sample code, but I'm getting error message in browser said "no database select"

 

please correct if this is right statment in (RED) text.

 

<?php

 

mysql_connect("localhost", "username", "password" "DatabaseName");

mysql_select_db("member");

 

function check_user_exist($username) {

$username = mysql_real_escape_string($username);

$query = mysql_query("SELECT * FROM userstable WHERE username = '{$username}'") or die(mysql_error());

$rows = mysql_num_rows($query);

 

if($rows > 0) {

  return false;

} else {

  return true;

}

}

 

if(check_user_exist($_POST["username"])) {

die("A user already exists with that username");

} else {

// Go ahead with the form processing

}

 

?>

Link to comment
Share on other sites

thank you for your help these mysql database site helped me lots, but I apologize this time, this must be confusing me, I have tried everything I could to solve problme but I couldn't find to solve problme.

 

what happend is that I recently add form with text field called usernamebut then I'm getting error message said A user already exists with that username with out any input in text filed.

 

what went wrong!

 

<?php

 

mysql_connect("localhost", "username", "password");

mysql_select_db("MemberDatabase");

 

function check_user_exist($username) {

$username = mysql_real_escape_string($username);

$query = mysql_query("SELECT * FROM member WHERE username = '{$username}'") or die(mysql_error());

$rows = mysql_num_rows($query);

 

if($rows > 0) {

  return false;

} else {

  return true;

}

}

 

if(check_user_exist($_POST["username"])) {

die("A user already exists with that username");

} else {

echo "thank you "

}

 

?>

 

<head>

 

<title>check user name</title>

</head>

 

<body>

<form id="form1" name="form1" method="post" action="">

  User Name:

  <label>

  <input type="text" name="userName" id="userName" />

  </label>

  <label>

  <input type="submit" name="button" id="button" value="Submit" />

  </label>

</form>

</body>

</html>

Link to comment
Share on other sites

To chigley;

 

hi again  :), I'm sorry i'm lost again, i made some change by put some text see the code:

 

 

}

 

if(check_user_exist($_POST["userName"])) {

die("A user already exists with that username");

} else {

echo ("<h1> <bold> Thank you </bold></hl>");

}

?>

 

But I'm getting same error message in browser said "A user already exists with that username".... I would appreication  your help can you explain what did i missed, i'm still rookie guy and learning php on my own but this part is confusing me.

 

 

 

AM

Link to comment
Share on other sites

Just a helpful tip, the [ code ] tags are your friends.

 

<?php
function check_user_exist($username) {
$username = mysql_real_escape_string($username);
$query = mysql_query("SELECT * FROM member WHERE username = '{$username}'") or die(mysql_error());
$rows = mysql_num_rows($query);


if($rows > 0) {
  return true;
} else {
  return false;
}
}

if(check_user_exist($_POST["userName"])) {
die("A user already exists with that username");
} else {
echo ("<h1> <bold> Thank you </bold></hl>");
}
?>

 

Reverse the true/false as how you are calling it that makes the logical sense.

 

So basically this will return true if the user exists, if the user does not exist it returns false, which is what you want as shown by the if statement.

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.