db_delete on click on a link in module contents display I've made a module displaying a set of fields from the database.
I would like to add a "delete" link at the end of each row be able to delete the row in the database on click. I made it in my module with the following code :

The tests.module code :  

&lt;?php   

function tests_menu(){
$items = array();

$items['tests'] = array(
    'title' =&gt; 'Tests',
    'page callback' =&gt; 'tests_contents',
    'access callback' =&gt; TRUE
);

$items['tests/delete/%'] = array(
    'title' =&gt; 'Delete',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('tests_delete_form', 1),
    'file' =&gt; 'tests_form.inc',
);
return $items;
}

function tests_contents(){
$delete_path = current_path();

$query = db_select('form', 'f');
$result = $query
        -&gt;fields('f')
        -&gt;execute()
        -&gt;fetchAll();

foreach($result as $key){
    $output .= $key-&gt;id;
    $output .= $key-&gt;name;
    $output .= '&lt;a href="'.$path.'/'.$key-&gt;id.'"&gt;download&lt;/a&gt;';
    $output .= '&lt;a href="'.$delete_path.'/delete/'.$key-&gt;id.'"&gt;delete&lt;/a&gt;';
} 
return $output;  
}


And the tests_form.inc code :

&lt;?php

function tests_menu(){
$items = array();

$items['tests/delete/%'] = array(
'title' =&gt; 'Delete',
'page callback' =&gt; 'drupal_get_form',
'page arguments' =&gt; array('tests_delete_form'),
'access callback' =&gt; TRUE
);
return $items;
}    

function tests_delete_form_submit($form, &amp;$form_state) {
$path = current_path();
$rest = substr($path, 13);

$query = db_select('form', 'f');
$result = $query
  -&gt;condition('f.id', $rest, '=')
  -&gt;fields('f')
  -&gt;execute();

$testid = $result-&gt;fetchField();

db_query("DELETE FROM {form} WHERE id = %testid", $testid);
}


How can I make this db_delete function on click in my module ?
Add path to delete your elements to hook_menu().

$items['something/%/delete'] = array(
  'title' =&gt; 'Delete',
  'page callback' =&gt; 'drupal_get_form',
  'page arguments' =&gt; array('mymodule_delete_form', 1),
);


In mymodule_delete_form(), use confirm_form(). It will generate "are you sure?" form for you. Then, just write mymodule_delete_form_submit() to perform actual deletion.