By stinis87 on
I have made a form, and when i fill it in and push the submit button, i get this message above: user warning: Duplicate entry '' for key 'PRIMARY' query: INSERT INTO hehe VALUES ('', '','','','') in C:\xampp\htdocs\drupal\sites\all\modules\hehe\hehe.module on line 77.
I have discovered the issue, but i dont know how to fix it. I'm trying to insert these form inputs into a table, but they are all "" even though i think they should have a value.. therfore i get this message because the second time i insert something, that primary key too is nothing(""),i'm just inserting emty fields into the table.
Heres the code:
function hehe_block($op='list', $delta=0) {
if ($op == "list") {
$block[0]["info"] = t("Hehe");
return $block;
} else if ($op == 'view') {
return vis_hehe();
brake;
}
}
function hehe_menu() {
$items = array();
$items['admin/form'] = array(
'title' => t('Arrangement'),
'page callback' => 'hehe_form',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function hehe_form() {
return drupal_get_form('lage_form');
}
function lage_form($form_state) {
$form = array();
$form['tittel'] = array(
'#type' => 'textfield',
'#title' => t('Tittel'),
'#description' => t('Tittel på arrangementet.'),
);
$form['beskrivelse'] = array(
'#type' => 'textfield',
'#title' => t('Beskrivelse'),
'#description' => t('Beskrivelse av arrangementet.'),
);
$form['sted'] = array(
'#type' => 'textfield',
'#title' => t('Sted'),
'#description' => t('Hvor befinner arrangementet seg.'),
);
$form['bruker'] = array(
'#type' => 'textfield',
'#title' => t('Lagt inn av'),
'#description' => t('Ditt brukernavn.'),
);
$form['type'] = array(
'#type' => 'select',
'#title' => t('Type arrangement'),
'#default_value' => '',
'#delta' => 10,
'#options' => array(
'konsert' => t('Konsert'),
'seminar' => t('Seminar'),
'fest' => t('Fest'),
'sport' => t('Sportsbegivenhet'),
'sosial' => t('Sosialt sammenkomst'),
),
'#description' => t('Hva slags type arrangement er dette.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#title' => t('Publiser'),
'#default_value' => 'Publiser',
);
return $form;
}
function lage_form_submit($form_id,$form_values) {
db_query("INSERT INTO {hehe} VALUES ('%s', '%s','%s','%s','%s')", $form_values['tittel'], $form_values['beskrivelse'],$form_values['sted'],$form_values['bruker'],$form_values['type']);
}
function vis_hehe() {
$resultat = db_query("SELECT * FROM {hehe} ORDER BY tittel");
$res = db_fetch_object($resultat);
$block['subject'] = $res->tittel;
$block['content'] = $res->beskrivelse;
return $block;
}
Anybody know what the problem might be?
Comments
try change function
try change
function lage_form_submit($form_id,$form_values) {
to
function lage_form_submit($form_id,&$form_values) {
thanks for the help:) i tried
thanks for the help:) i tried though, no luck...the same thing happened. it seems right doesnt it?
try this hook_form
try this hook_form syntax:
hook_form(&$node, $form_state)
yeah i noticed that in drupal
yeah i noticed that in drupal 6.x u have two arguments. i changed it to hook_form(&$node, $form_state), and now that warning is replace by warning: Missing argument 2 for lage_form() in C:\xampp\htdocs\drupal\sites\all\modules\hehe\hehe.module on line 30.
try this hook_form
what is line 30?
i found the problem, but then
i found the problem, but then i got back to the first warning again, im stuck...
post your .install file
post your .install file ...
let me fix it ..
I post both files here so you
I post both files here so you can see the whole thing.
// hehe.module
//hehe.install
can u try the following changes
If u are coding in D6 as I can see at the top of the page.
Try this code(this code is not tested)
One more thing I noticed that in ur menu function ur calling
function hehe_form()and in that function ur calling
drupal_get_form('lage_form',$form_state);So what I would suggest is comething like this
This code above will help us get rid of the
function hehe_form()I believe the
function lage_form(&$node,$form_state)should look something
function lage_form($form_state)For more documentation refer to
http://api.drupal.org/api/file/developer/topics/forms_api.html/6
http://api.drupal.org/api/file/developer/topics/forms_api_reference.html/6
Try the above mentioned changes and let me know.
ooh maan, it worked!:D thanks
ooh maan, it worked!:D thanks alot!:D i just changed my submit function with yours
function lage_form_submit($form, &$form_state) {
$datenow = mktime(0,0,0,date("m"),date("d"),date("Y"));
db_query("INSERT INTO {hehe} VALUES ('%s', '%s','%s','%s','%s','%s')",$form_state['values']['tittel'], $form_state['values']['beskrivelse'], $form_state['values']['sted'], $form_state['values']['bruker'], $form_state['values']['type'], $datenow);
drupal_set_message(t("Arrangementet er publisert"));
}
i didnt notice that _id behind the form there:P u are genious:)
thanks to both of you for the help, finally i can proceed..
bt the way.. can you see any
bt the way.. can you see any reason why my hook_block functions wont work? i cant seem to make it show the entered input in a block..
change the function as follows and let me know
Just try this and let me know if it works or not .
One more thing I noticed is that ur not using hook_schema and ur using only
hook_install and hook_uninstall to create and delete table.
It will work but it is not the right way to do it.
Refer to http://drupal.org/node/323314
http://drupal.org/node/146843
for more info on how to write the install file