Hi

I am using 4.7.0-beta3 and I want to add another textfield to other nodes, say "page", then add to the edit form and save it into a table I created. I think it should be pretty simple but I've missed something and would really appreciate an expert looking over it quickly - should be obvious.

I use nodeapi and form_alter hooks as I understand it - the field I want is a syllabus field for a page so I have done the following:

function fhsstadmin_form_alter($form_id, &$form) {
  if (isset($form['type'])) {
    $node = $form['#node'];

    if ($form['type']['#value'] .'_node_settings' == $form_id) {
      $form['fhsstadmin'] = array('#type' => 'fieldset', '#title' => t('Syllabus'));
      $form['fhsstadmin']['fhsstadmin_options_'. $form['type']['#value']] = array(
        '#type' => 'radios',
        '#title' => t('Syllabus'),
        '#default_value' => variable_get('fhsstadmin_options_'. $form['type']['#value'], 1),
        '#options' => array(t('Disabled'), t('Enabled')),
        '#description' => t('Choose whether syllabus should be shown or not.')
      );
    }

    if ($form['type']['#value'] .'_node_form' == $form_id && variable_get('fhsstadmin_options_'. $node->type, 1)) {
      $form['fhsstadmin_syllabus'] = array(
        '#type' => 'textarea',
        '#title' => t('Syllabus'),
        '#default_value' => $node->syllabus,
        '#cols' => 60,
        '#rows' => 5,
        '#weight' => -17.5,
        '#description' => t('Enter the syllabus for this page.')
      );
    }

  }
}


function fhsstadmin_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'delete':
        db_query("Delete * FROM fhsstadmin_syllabus WHERE nid = $node->nid");
      break;
    case 'load':
        $syllabus = db_fetch_object(db_query("SELECT * FROM fhsstadmin_syllabus WHERE nid = $node->nid"));
        return array('syllabus' => $syllabus->syllabus);
      break;
    case 'insert':
        db_query("INSERT INTO fhsstadmin_syllabus (nid, syllabus) VALUES (%d,%s)",$node->nid,$node->syllabus);
      break;
    case 'update':
        db_query("INSERT INTO fhsstadmin_syllabus (nid, syllabus) VALUES (%d,%s)",$node->nid,$node->syllabus);
      break;
  }
}

Now it shows up when I create a page and I can type stuff into a "syllabus" block but on submission I get:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 query: INSERT INTO fhsstadmin_syllabus (nid, syllabus) VALUES (2, ) in /var/www/FHSST2/includes/database.mysql.inc on line 108.

Which I think is telling me that $node->syllabus doesn't come back with anything once the form has been submitted.

What is the correct way to accept the value back from the form and store it in my table?

Thanks,

Mark

Comments

codexmas’s picture

It looks like the error is at the end of your SQL statement:

VALUES (2, )

If the second paramater is a string, you should be making the query like this:

"INSERT INTO fhsstadmin_syllabus (nid, syllabus) VALUES (%d,'%s')"

Gord.

marknewlyn’s picture

I still don't get the value from the syllabus back into the
database.

Any idea if the way I have written the form is incorrect?

Thanks,

Mark

codexmas’s picture

This line seems to be returning a single element from the object fetched from the table you created?

return array('syllabus' => $syllabus->syllabus);

You are effictively returning only one of the fields from the database by doing this. Then when you are creating your INSERT query, this text result is getting cast as an integer.

Change the code to something like this:

return db_fetch_array(db_query("SELECT nid, syllabus FROM fhsstadmin_syllabus WHERE nid = $node->nid"));

And remove the return below that line.

Try that...

Good luck!
Gord.

marknewlyn’s picture

When I create a new page its happily calling:

db_query("INSERT INTO {fhsstadmin}_syllabus (nid, syllabus) VALUES (%d, '%s')",$node->nid,$node->syllabus);

but $node->syllabus is completely empty :-/ (even after making the previous change you suggested).

I think that I've screwed something up in the form_alter - I have been playing a bit but its not helping, I currently have empty strings being stored in the database so I think my form or how to respond to it is wrong:

mysql> select * from fhsstadmin_syllabus;
+----------+-----+
| syllabus | nid |
+----------+-----+
|          |   2 |
|          |   2 |
|          |   2 |
|          |   4 |
|          |   5 |
|          |   6 |
|          |   7 |
|          |   8 |
+----------+-----+
8 rows in set (0.00 sec)
marknewlyn’s picture

I would really appreciate someone pointing out what rule is now obeyed in the code to follow. I think that what I really changed was to append my module name to the field that was being added to the node but this can't actually be a rule can it?

Either way I am happy to plug on ... I am enjoying learning about and using Drupal just for the record - but perhaps its just a case of ignorance is bliss :)

function fhsstadmin_form_alter($form_id, &$form) {
  if (isset($form['type'])) {
    $node = $form['#node'];

    if ($form['type']['#value'] .'_node_settings' == $form_id) {
      $form['fhsstadmin'] = array('#type' => 'fieldset', '#title' => t('Syllabus'));
      $form['fhsstadmin']['fhsstadmin_options_'. $form['type']['#value']] = array(
        '#type' => 'radios',
        '#title' => t('Syllabus'),
        '#default_value' => variable_get('fhsstadmin_options_'. $form['type']['#value'], 1),
        '#options' => array(t('Disabled'), t('Enabled')),
        '#description' => t('Choose whether syllabus should be shown or not.')
      );
    }

    if ($form['type']['#value'] .'_node_form' == $form_id && variable_get('fhsstadmin_options_'. $node->type, 1)) {
      $form['fhsstadmin_syllabus'] = array(
        '#type' => 'textarea',
        '#title' => t('Syllabus'),
        '#default_value' => $node->fhsstadmin_syllabus,
        '#cols' => 60,
        '#rows' => 5,
        '#weight' => -17.5,
        '#description' => t('Enter the syllabus for this page.')
      );
    }

  }
}


function fhsstadmin_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'fields':
      return array('syllabus');
      break;
    case 'delete':
        db_query("Delete * FROM fhsstadmin_syllabus WHERE nid = $node->nid");
      break;
    case 'load':
        //return db_fetch_array(db_query("SELECT nid, syllabus FROM fhsstadmin_syllabus WHERE nid = $node->nid"));
        $syllabus = db_fetch_object(db_query("SELECT * FROM fhsstadmin_syllabus WHERE nid = $node->nid"));
        return array('fhsstadmin_syllabus' => $syllabus->syllabus);
      break;
    case 'insert':
        db_query("INSERT INTO {fhsstadmin}_syllabus (nid, syllabus) VALUES (%d, '%s')",$node->nid,$node->fhsstadmin_syllabus);
      break;
    case 'update':
        db_query("INSERT INTO {fhsstadmin}_syllabus (nid, syllabus) VALUES (%d, '%s')",$node->nid,$node->fhsstadmin_syllabus);
      break;
  }
}