Hi all,

appologies in advance, since I am a newbie to Dev. I am trying to write a new content type. I guess I could just use the CCK in order to do this, but I thought I would have a bash at developing a module to do it, more as an exercise.

Anyhow, I managed to write an install file with the following for hook_schema:

function booking_admin_schema() {
  $schema['booking_admin'] = array(
    'fields' => array(
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'start_date' => array(
        'type' => 'datetime',
      ),
       'places' => array(
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
    ),
    'primary key' => array('vid'),
    );
  return $schema;
}

Anyhow, I am having issues trying to insert the date and the places into the table that my install file creates. The hook_insert looks like the following:

function booking_admin_insert($node) {
  
  ///adds a default string for the path column
  if (!isset($node->start_date)) {
      $node->start_date = '';
    }
  if (!isset($node->places_available)) {
      $node->places_available = '';
    }
  
  ////SQL INSERT statement
  db_query('INSERT INTO {booking_admin} (vid, nid, start_date, places) VALUES (%d, %d, %s %s)',
    $node->vid,
    $node->nid,
    $node->start_date,
    $node->places
    );
echo "<br>sql done<br>";
}

I am using the following in the hook_form function in order to achieve a popup menu:

 $form['start_date'] = array(
    '#type' => 'date_popup',
    '#title' => t('Start Date'),
    '#description' => t('This is the start date for the week, which should be a saturday'),
    '#default_value' => isset($node->start_date) ? $node->start_date : '',
    );

Anyhow can anyone tell me where I am going wrong.

Thanks again in advance of any posts,

Mark,

Comments

mattyoung’s picture

I just google "drupal db_query insert date" and found that. It appears you need to format the date as a string and insert using '%s'.

B14speedfreak’s picture

Thanks for that, I will have ago with it a bit later on...

Thanks again,

Mark.

kovalev’s picture

By the way, these string-dates may cause a bug, when you are comparing them to sql dates. If you have a "created" column in table of a varchar type, in which you inserted a value from phps date function, a query : "SELECT something FROM somewhere WHERE created>now()" will give you unexpected sets of results.
So its better to store as unixtime, or at least when comparing dates, write: "SELECT something FROM somewhere WHERE UNIX_TIMESTAMP(created)>UNIX_TIMESTAMP(now())"
------------------------------------------------------------------------------------------------------------------------------------
Sergata - פיתוח תוכנה