I am now taking on the challenge of building a custom module.
I have started really small just to try and the understanding of drupal hooks and modules.
I have created a simple form with one text entry, the only validation is that the field is not empty.
On the form submit I would like to write to a custom table. The table exists.
My fields are:
nid int(11)
eid int(11) Primary Key Auto Increment
title varchar(50)
Here is my form:
function my_module_my_form($form_state) {
$form['esp'] = array(
'#type' => 'fieldset',
'#title' => t('Add a ESP'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['esp']['title'] = array(
'#type' => 'textfield',
'#title' => t('ESP Name'),
'#required' => TRUE,
'#default_value' => '',
'#description' => "Enter the ESP Name",
'#size' => 20,
'#maxlength' => 20,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
and here is the hook_submit()
that I have setup:
function my_module_my_form_submit($form, $form_state) {
db_query("INSERT INTO my_module_esp (title) VALUES (".$form_state['values']['title'].")");
drupal_set_message(t('The form has been submitted.'));
}
But this does not work... any suggestions? Also I would like the add the latest nid to this table, how would I get that value and update it in the db so it does not affect any other modules?
Any help would be much appreciated
You need to take the form's hierarchy into account. Try using:
If you need to to assign this a nid, then you should actually create a node in your code so that 301 is really associated with something. The primary key in the node table is auto_increment, so if you don't create a node in this code, then someone else could go create a blog post and would get assigned 301. Use node_save() to create a node