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

mm167’s picture

try change

function lage_form_submit($form_id,$form_values) {

to

function lage_form_submit($form_id,&$form_values) {

stinis87’s picture

thanks for the help:) i tried though, no luck...the same thing happened. it seems right doesnt it?

mm167’s picture

try this hook_form syntax:

hook_form(&$node, $form_state)

stinis87’s picture

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.

mm167’s picture

what is line 30?

stinis87’s picture

i found the problem, but then i got back to the first warning again, im stuck...

mm167’s picture

post your .install file ...
let me fix it ..

stinis87’s picture

I post both files here so you can see the whole thing.

// hehe.module


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',$form_state);
}

function lage_form(&$node,$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) {
 	$datenow = mktime(0,0,0,date("m"),date("d"),date("Y"));
	
	db_query("INSERT INTO {hehe} VALUES ('%s', '%s','%s','%s','%s','%s')",$form_values['tittel'], $form_values['beskrivelse'],$form_values['sted'],$form_values['bruker'],$form_values['type'],$datenow);
	drupal_set_message(t("Arrangementet er publisert"));

}

function vis_hehe() {
	$resultat = db_query("SELECT * FROM {hehe} ORDER BY time");
	$res = db_fetch_object($resultat);
	
	$block['subject'] = $res->tittel;
    $block['content'] = $res->beskrivelse;
    return $block;
  }

//hehe.install

/*
function hehe_schema() {

  $schema['hehe'] = array(
    'description' => t('Arrangement'),
    'fields' => array(
      'tittel' => array(
        'type' => 'text',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'beskrivelse' => array(
        'type' => 'text',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'sted' => array(
        'type' => 'text',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''),
      'bruker' => array(
        'type' => 'text',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
     'type' => array(
        'type' => 'text',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''),
	  ),
    'indexes' => array(
      'hehe_changed' => array('changed'),
      'hehe_created' => array('created'),
      ),
    'unique keys' => array(
      'tittel_sted' => array('tittel', 'sted'),
      'sted' => array('sted')
      ),
    'primary key' => array('tittel'),
   );
   return $schema;
}	 
	*/ 
//function hehe_install() {
  //drupal_install_schema('hehe');
//}

function hehe_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      
      db_query("CREATE TABLE {hehe} (
          tittel varchar(255),
          beskrivelse text,
		  sted text,
		  bruker text,
		  type text,
		  time text,
          PRIMARY KEY(tittel)
       ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;

    case 'pgsql':
      db_query("CREATE TABLE {hehe} (
          tittel varchar(255),
          beskrivelse text,
		  sted text,
		  bruker text,
		  type text,
		  time text,
          PRIMARY KEY(tittel)
       	  )");

      db_query("CREATE INDEX {hehe}_tittel_idx
                ON {hehe} (tittel)");
      break;
  }
}

function hehe_uninstall() {
  	switch ($GLOBALS['db_type']) {
    	case 'mysql':
    	case 'mysqli':
  			db_query('DROP TABLE {hehe}');
 			break;
		case 'pgsql':
			db_query('DROP TABLE {hehe}');
 			break;
	}		
}
junedkazi’s picture

If u are coding in D6 as I can see at the top of the page.
Try this code(this code is not tested)


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"));

}

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


function hehe_menu() {
  $items = array();
  $items['admin/form'] = array(
    'title' => t('Arrangement'),
    'page callback' => 'drupal_get_form',
    'page arguments' = array('lage_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

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.

stinis87’s picture

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..

stinis87’s picture

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..

junedkazi’s picture

function vis_hehe() {
    $resultat = db_query("SELECT * FROM {hehe} ORDER BY time");
    $res = while(db_fetch_object($resultat)) {
       $block[]['subject'] = $res->tittel; 
       $block[]['content'] = $res->beskrivelse;
   }
    //try this return or the one below it which is commented
    return $block;
    //return theme('item_list', $block);
  }

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