Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8579121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:32:42+00:00 2026-06-11T20:32:42+00:00

Possible Duplicate: Insert submitted form content to DB using drupal_write_record I’m new in drupal

  • 0

Possible Duplicate:
Insert submitted form content to DB using drupal_write_record

I’m new in drupal and what I trying to do is my first module so hope you understand me.
I created a registration module, and want to connect it to database through drupal_write_record but I don’t know exactly how to do it. I read how to use this method from drupal website but still couldn’t do it.

Can you please help me manage that?

Here is how my module looks like:

function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
    'title' => 'Contact Information',
    'description' => 'Contact Information',
    'page callback' => 'drupal_get_form',
    'access callback' => true,
    'weight' => '10',
    'page arguments' => array('my_module_form'),
    'access arguments' => array('access content'),
    'type' =>  MENU_NORMAL_ITEM,
);
$items['my_module/list/1'] = array(
    'title' => t('Contact list'),
    'page callback' => 'my_module_form_list_page',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK, 
);
return $items;
}
function my_module_perm() {
return array('access my_module content', 'access administration pages');
}

function my_module_help($path, $arg) {
$output = '';  //declare your output variable
switch ($path) {
case "admin/help#my_module":
    $output = '<p>'.  t("Displays information about site") .'</p>';
    break;
}
return $output;
}
function my_module_form($form_data) {
$form['contact_information'] = array(
    '#value' => variable_get('contact_form_information', t('Please fill in all fields.'))
);
$form['first'] = array(
    '#type' => 'textfield',
    '#title' => t('First name'),
    '#maxlength' => 20,
    '#required' => TRUE,
);
$form['last'] = array(
    '#type' => 'textfield',
    '#title' => t('Last name'),
    '#maxlength' => 20,
    '#required' => TRUE,
);
$form['street'] = array(
    '#type' => 'textfield',
    '#title' => t('Street Address'),
    '#required' => TRUE,
); 
$form['city'] = array(
    '#type' => 'textfield',
    '#title' => t('City'),
    '#required' => TRUE,
); 
$form['postal'] = array(
    '#type' => 'textfield',
    '#title' => t('Postal Code'),
); 
$form['state'] = array(
    '#type' => 'select',
    '#title' => t('State'),
    '#default_value' => variable_get('feed_item_length','chisinau'),
    '#options' => array(
    'chisinau' => t('Chisinau'), 
    'balti' => t('Balti'), 
    'cahul' => t('Cahul')),
); 
$form['phone'] = array(
    '#type' => 'textfield',
    '#title' => t('Business Phone'),
    '#required' => TRUE,
); 
$form['ext'] = array(
    '#type' => 'textfield',
    '#title' => t('ext'),
); 
$form['mobile'] = array(
    '#type' => 'textfield',
    '#title' => t('Mobile'),
    '#required' => TRUE,
); 
$form['home_phone'] = array(
    '#type' => 'textfield',
    '#title' => t('Home Phone'),
);
$form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#required' => TRUE,
);
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
);
$form['clear'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#validate' => array('my_module_my_form_clear'),
);
return $form;
}

function my_module_form_submit($form, &$form_data){
global $user;
    $data = array(
        'id' => $user->id,
        'first' => $form_data['values']['first'],
        'last' => $form_data['values']['last'],
        'street' => $form_data['values']['street'],
        'city' => $form_data['values']['city'],
        'postal' => $form_data['values']['postal'],
        'state' => $form_data['values']['state'],
        'phone' => $form_data['values']['phone'],
        'ext' => $form_data['values']['ext'],
        'mobile' => $form_data['values']['mobile'],
        'home_phone' => $form_data['values']['home_phone'],
        'email' => $form_data['values']['email'],
    );
drupal_write_record('my_module', $data ,'id');
drupal_set_message(t('Information has been saved to the database')); 
}

working with this method, but is not what I need:

function my_module_form_submit($form_id, &$form_data)  {
db_query("INSERT INTO my_module (id, first, last, street, city, postal, state, phone, ext, mobile, home_phone, email)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
$form_data['values']['first'],
$form_data['values']['last'], 
$form_data['values']['street'],
$form_data['values']['city'],
$form_data['values']['postal'], 
$form_data['values']['state'],
$form_data['values']['phone'],
$form_data['values']['ext'], 
$form_data['values']['mobile'],
$form_data['values']['home_phone'],
$form_data['values']['email']); 
drupal_set_message(t('Information has been saved to the database')); 
}

My_module.install file :

function my_module_install() {
  drupal_install_schema('my_module');
}

function my_module_uninstall() {
 drupal_uninstall_schema('my_module');
  } 

function my_module_schema(){
  $schema = array();
  $schema['my_module'] = array(
  'description' => 'My Module table.',
  'fields'=>array(  
  'id'=>array(  
  'type'=>'serial',  
  'unsigned'=>TRUE,  
  'not null'=>TRUE  
  ),  
  'first'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),  
  'last'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ), 
  'street'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'city'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'postal'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'state'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'phone'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'ext'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ), 
  'mobile'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'home_phone'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'email'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  ),  
   'primary key'=>array("id"), 
  );
  return $schema;
  }

What is wrong and how to solve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:32:43+00:00Added an answer on June 11, 2026 at 8:32 pm

    The third parameter of drupal_write_record should be an array of the primary ids. In your case (if the field id is really the primary key.) should be array('id'). However, since i cannot see the whole hook_schema function I can’t say if you have a primary key set?

    Are you using Drupal 7 or Drupal 6? Drupal 6 also requires you to have a hook_install() with a function call to drupal_install_schema('my_module').

    Read more about drupal_write_record here: http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_write_record/7.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to get the insert ID in JDBC? Hi, I'm using JDBC
Possible Duplicate: oracle insert if row not exists I am using the query below
Possible Duplicate: SQL Server 2008 : Cannot Insert new column in the middle position
I'm trying to validate uniqueness of an entity submitted from a form by using
Possible Duplicate: PHP SQL Form Insert I made a form to take input and
Possible Duplicate: Using ggplot2, can I insert a break in the axis? I'm using
Possible Duplicate: Insert string between two points with PHP How can I replace everything
Possible Duplicate: Bulk Insert of hundreds of millions of records So I was just
Possible Duplicate: Fetch data in database table insert it if not exist else return
Possible Duplicate: PHP htmlentities() on input before DB insert, instead of on output For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.