Hi
I have problem with form which I have created after reading the form API and all the other stuff about forms. I am trying to create one sample form to save user input into a database table. For the sake of understanding the process of form development I have tried to use textfield, date, select, radios and submit button. To insert this data into the database table I am using db_query. Most of the data is going to the database table except the “date”. I have tried many options to resolve this problem but unable to solve it. Following is the table structure;

CREATE TABLE `emp` (
`emp_id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(45) NOT NULL default '',
`designation` varchar(45) NOT NULL default '',
`dob` date NOT NULL default '0000-00-00',
`division` varchar(45) NOT NULL default '',
`gender` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='test table';

Following is the test module for creating form;

 //Code Starts here-----------
<?php
// $Id$
/**
* Implementation of hook_menu().
*/
function testform_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'testform',
'title' => t('The Test Form'),
'callback' => 'testform_page',
'access' => TRUE
);
}
return $items;
}
/**
* Called when user goes to example.com/?q=testform
*/
function testform_page() {
$output = t('This page contains our Employee Data Entry Form.');
// Return the HTML generated from the $form data structure.
$output .= drupal_get_form('testform_nameform');
return $output;
}

/**
* Defines a form.
*/
function testform_nameform() {

$form['emp_details']['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#description' => t('Please enter your name.'),
'#required' => TRUE
);

//EMP designation
$form['emp_details']['designation'] = array(
'#title' => t('Designation'),
'#type' => 'textfield',
'#description' => t('Please enter your designation.'),
'#required' => TRUE
);

//EMP dob
$form['emp_details']['dob'] = array(
'#title' => t('Date of Birth'),
'#type' => 'date',
'#description' => t('Please enter your date of birth.'),
'#required' => TRUE,
'#default_value' => array(
'year' => format_date(time(), 'custom', 'Y'),
'month' => format_date(time(), 'custom', 'n'),
'day' => format_date(time(), 'custom', 'j')
)
);

//EMP division
$form['emp_details']['division'] = array(
'#title' => t('Division'),
'#type' => 'select',
'#required' => TRUE,
'#options' => array(t('div-1'), t('div-2'), t('div-3')),
'#description' => t('Please select your division.'),
);

//EMP gender
$form['emp_details']['gender'] = array(
'#title' => t('Gender'),
'#type' => 'radios',
'#required' => TRUE,
'#description' => t('Please select your gender.'),
'#options' => array(
t('Male'),
t('Female')
),
);

//Submit Button
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save')
);
return $form;
}
/**
* Handle post-validation form submission.
*/
function testform_nameform_submit($form_id, $form_values) {
  db_query("INSERT INTO {emp} (name, designation, dob, division, gender) VALUES ('%s', '%s', %d, '%s', '%s')", $form_values['name'], $form_values['designation'], $form_values['dob'], $form_values['division'], $form_values['gender']);
  drupal_set_message(t('Your form has been saved.'));
}
//Code ends here -----------
?>

Please help me in understanding and creating forms in Drupal.
Thanks in advance…

Comments

VivekDubey’s picture

One more thing, whenever I submit this form the default value of date i.e. 0000:00:00 is posted to the database table, irrespective of what I choose in the date control.

please help...

naveenpl’s picture

If you have a _validate function(hook) then just look at you output for print_r($form-value['dob'])
you can see the output in mm;dd:yyyy , so before saving I think you need to change format to yyyy:dd;mm.
Didn't tried to investigate much into it.
This link would be helpfull
http://drupal.org/node/71962

Hope this will help
Cheers.

VivekDubey’s picture

Thanks for your suggestion,
I have tried it and the value returned from $form_values['dob'] is “Array”.
Now how do I reference the values of year, month and date?
Following is what i used in validate function;

function testform_nameform_validate($form_id, $form_values) {
if ($form_values['name'] == 'a') {
    form_set_error('dob',t('date is '.$form_values['dob']));
}
}

mooffie’s picture

the value returned from $form_values['dob'] is “Array”

This array holds three elements. First, concatenate them into a nice string:

$dob = $form_values['dob']['year'] .'-'. $form_values['dob']['month'] .'-'. $form_values['dob']['day'];

This will give us a '1973-10-03' string, which is the format databases expect.

Then write it out to the database; use the '%s' placeholder:

db_query("INSERT ......... '%s' ........", ....... $dob.......);
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='test table';

Better use 'utf8', not 'latin'. (And all other unnecessary directives there should go.)

VivekDubey’s picture

Thanks
It did worked and your suggestions helped me in understanding the things better…
Thank you very much.

skamaraj1981’s picture

Hai i have used '%s' and changed character set as utf8. But i'm not able to insert date in table. It always insert '0000-00-00'. Please give me a solution

ldbl’s picture

This is working piece of code.

db_query("INSERT INTO {testform} 
(birthdate) 
VALUES 	('%s')",
$form_values['birthdate']['year'] . '-' . $form_values['birthdate']['month'] . '-' . $form_values['birthdate']['day'] 
);

This means in this example format is year-month-day.

Ambreen Darjat’s picture

I am inserting a creation date of node 'project'. This is how I am doing it, but somehow it always inserts '0000-00-00'.

function project_form(&$node, $form_state)
{
$form ['ProjectStart'] = array(
     '#type' => 'date', 
     '#title' => t('Project Start Date'), 
     '#description' => t('Enter the date at which the project started'), 
     '#required' => TRUE
	 );
return $form;
}
function project_insert($node)
{
   db_query("INSERT INTO {rms_project} (ProjectStart) VALUES ( %d)",  $node->ProjectStart);
}
skamaraj1981’s picture

Hi i'm using this query "db_query("INSERT INTO {emp} (name, designation, dob, division, gender) VALUES ('%s', '%s', %d, '%s', '%s')", $form_values['name'], $form_values['designation'], $form_values['dob'], $form_values['division'], $form_values['gender']);". when i use this division, and gender fields values are entered as '0' or '1' in tables, but i need to insert div-1, div-2, male and female. How to do?

sanjeevchadha’s picture

hi I am using multistep form and for that I am displaying 5 date fields , the problem is when the form appears current date is being displayed on the form..
Instead I want "--Year--" "-- month --" "-- date --" strings to display first and then the values beneath them. Plz help this is something urgent I want to cover up

Many Thanks
In advance