I have website and on its home page there is form in which user enter his own label .As user enters label if the user with same label exists in database it through message that "This Label already exists.Use other Label OR Login". If the user is new it will be directed to other site i.e site "example.com".

The problem is I am getting the same message that "This Label is already exist.Use other Label OR Login" even I enter a new label.

Here is the code for this scenario.

function homepage_userlogin_form($form_state){
// drupal_set_title('Add New Person');
$form = array();

$form['name'] = array(
'#title' => '',
'#type' => 'textfield',
'#size' => '30',
'#placeholder' => "enter desired label",

$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Start',

//$form['#attributes'] = array('id' => 'user-registration001');    
return $form;

function homepage_userlogin_form_validate($form, &$form_state) {

$name = $form_state['values']['name'];   
global $users;

if (empty($name)) {
form_set_error("erorr",t("Please enter your Label."));
// form_set_error("Please Enter atleast 6 character Label.");
// }


$result = db_select('users', 'u')
->condition('name', $name,'=')

form_set_error("erorr1",t("This Label is already exist.Use other Label OR <a  

global $users;


function homepage_userlogin_form_submit($form, &$form_state) {


2 Answers 2


I think there is problem with the condition in the query. Try changing it

$result = db_select('users', 'u')


->condition('u.name', $name,'=')



You better use Webform Module to create a form without any issues!!

Webform is the module for making surveys in Drupal. After a submission, users may be sent an e-mail "receipt" as well as sending a notification to administrators. Results can be exported into Excel or other spreadsheet applications. Webform also provides some basic statistical review and has and extensive API for expanding its features.


The title of OP seems misleading. The code is already connecting to the db on form submission. Perhaps revise the title? Unique usernames are default behaviour, so I'm assuming you're trying to do something different here. It's difficult to debug without running the code. This part looks problematic:

  1. You don't need to check uid, you just need to see whether the db query returns a result, in which case you could use $result->rowCount()
  2. What do you get if you echo $result['uid']?

General comments:

  • You could consolidate your conditional logic. You possibly don't need to test for $error: if(){}else{} statements could be combined, or you could just return FALSE.
  • You're calling global $user twice.
  • Further down the script, you attempt to set the username in the global array, but that's not going to affect anything permanent, and you're basically saying that if the user exists, set their username to the current username, so I don't see how this changes anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.