My SQL syntax is apparently wrong, but I can't figure out why.

*.module file:

<?php
function foo_generation() {
  
  global $user;
  $nid = $form_state['values']['nid'];
  $uid = $form_state['values']['uid'];
  $result = db_query('SELECT like FROM {foo} WHERE nid = %d AND uid = %d', $node->nid, $user->uid);
  $like_original = db_result($result);
  return $like_original;
  
}

// ...
// Part of hook_form()

  // Define a submit function.
  $form['foo']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Like'),
    '#description' => foo_generation() . t('text string.'),
  );

// ...
// Part of hook_form_submit()

  global $user;
  $nid = $form_state['values']['nid'];
  $like = foo_generation() + 1;
  db_query("INSERT INTO {foo} (nid, uid, like) VALUES
  (%d, %d, %d)", $nid, $user->uid, $like);


?>

*.install file:

<?php
// The hook_schema()
function foo_schema() {
$schema['foo'] = array(
'description' => t('Stores number of \'foo\' for a node'),
'fields' => array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {node}.nid to which the \'like\' applies.'),
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {user}.uid of the user who \'foo\' the node.'),
),
'like' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The number of \'likes\' for a node.')
),
),
'primary key' => array(
'nid', 'uid'
),
);
return $schema;
}
?>

Comments

nevets’s picture

I would guess it has to do with using like as a field name since like is a sql keyword.

yuvraj_jain’s picture

All Is Well except the column name change the column name 'like' to anything else.
I think That you cannot give the name of your column 'like' because it is a keyword used by the MYSQL.

Anonymous’s picture

use 'drupal_write_record' instead

http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_wr...

 $data = array(
    'fieldname1' => $value1,
    'fieldname2' => $value2,
    'fieldname3' => $value3,
    'fieldname4' => $value3
  );
  drupal_write_record('your_table_name', $data);