1

I have created a module but my code gives me an error

Undefined index: db_type in student_install()

How can I fix this?

Following are my three files

student.INFO FILE

name        = student
description = "Add/edit/save student data."
core        = 7.x
package = "students"

students.MODULE FILE

<?php

/**
 * Students - The text editor for the Internet - http://ckeditor.com
 * Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
 *
 */

 function students_menu(){
 $items = array();
  // path location (<a href="http://www.url.com/foo/bar" title="http://www.url.com/foo/bar">http://www.url.com/foo/bar</a> )
  $items[] = array(
    //path
    'path'=> 'admin/settings/students',
    // page title
    'title' => t('Manage Students'),
    // describe the page for the menu system.  site visitors will not see this
    'description' => ('Add students'),
    // function that is called when visiting the new path
    'callback' => 'drupal_get_form',
    'callback arguments' => 'Students settings',
    // permissions required to view page
    'access' => user_access('access administration pages'),
  );  
  return $items;
}
 function students_settings()
 {
    $form['name'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'What is Your Name?',
    '#size' => 20,
    '#maxlength' => 20,
    '#required' => TRUE, //make this field required 
  );
  $form['last_name'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'What is Your Last Name?',
    '#size' => 10,
    '#maxlength' => 10,
    '#required' => False, //make this field required 
  );
  $form['email'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'What is Your email?',
    '#size' => 20,
    '#maxlength' => 20,
    '#required' => TRUE, //make this field required 
  );
  $form['age'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'What is Your Age?',
    '#size' => 10,
    '#maxlength' => 10,
    '#required' => TRUE, //make this field required 
  );
  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Click Here!'),
  );
  return system_settings_form($form);

 }
 function students_get_form($form_id,$form_values)
 {
    drupal_set_message(t("Submited"));
 }

students.INSTALL FILE

function students_install(){
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      // the {tablename} syntax is so multisite installs can add a
      // prefix to the table name as set in the settings.php file
      db_query("CREATE TABLE node_field_students_data (
          id int unsigned NOT NULL default '0',
          name varchar(32) NOT NULL default '',
          age int unsigned NOT NULL default '0',
          roll_num int unsigned NOT NULL default '0',
          PRIMARY KEY  (id)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;

    case 'pgsql':
      db_query("CREATE TABLE node_field_students_data (
          id serial CHECK (id >= 0),
          name varchar(32) NOT NULL default '',
          age integer NOT NULL default '0' CHECK (age >= 0),
          roll_num integer NOT NULL default '0' CHECK (roll_num >= 0),
          PRIMARY KEY  (id)
        )");

      // Pgsql requires keys and indexes to be defined separately.
      // It's important to name the index as {tablename}_fieldname_idx
      // (the trailing _idx!) so update scripts can be written easily
      db_query("CREATE INDEX node_field_students_data name_idx
                ON node_field_students_data (name)");
      break;
  }
}

function students_uninstall()
{
    switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      // the {tablename} syntax is so multisite installs can add a
      // prefix to the table name as set in the settings.php file
      db_query("DROP TABLE node_field_students_data");
      break;

    case 'pgsql':
      db_query("DROP TABLE node_field_students_data");

      // Pgsql requires keys and indexes to be defined separately.
      // It's important to name the index as {tablename}_fieldname_idx
      // (the trailing _idx!) so update scripts can be written easily
      break;
  }
}
1

The $db_type variable no longer exists in Drupal 7. You need to use template code that was designed for Drupal 7.

  • 1
    Generally speaking you can find the same value in $GLOBALS['databases']['default']['default']['driver']. But obviously the fact there are naked SQL statements being run in hook_install(), along with manual creation of tables that should have been created my CCK, means the code has many, many problems, and as you say needs to be upgraded (totally ripped out and started again in this case) – Clive May 21 '15 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.