how to connect to database for form submission 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' =&gt; '',
'#type' =&gt; 'textfield',
'#size' =&gt; '30',
'#placeholder' =&gt; "enter desired label",
); 

$form['submit'] = array(
'#type' =&gt; 'submit',
'#value' =&gt; 'Start',
"#submit"=&gt;array('form_submit')
);

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

function homepage_userlogin_form_validate($form, &amp;$form_state) {

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

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

if(!$error){

$result = db_select('users', 'u')
-&gt;fields('u',array('uid'))
-&gt;condition('name', $name,'=')
-&gt;execute()-&gt;fetchAssoc();

if($result['uid']&gt;0){
form_set_error("erorr1",t("This Label is already exist.Use other Label OR &lt;a  
href='user/login?user_label=".$name."'&gt;Login&lt;/a&gt;"));
}

else{
global $users;
$user-&gt;name=$name;

}
}
}

function homepage_userlogin_form_submit($form, &amp;$form_state) {
drupal_goto("http://example.com");

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

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

-&gt;fields('u',array('uid'))

-&gt;condition('u.name', $name,'=')

-&gt;execute()

-&gt;fetchField();


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.