By saturdayllc on
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
I would guess it has to do
I would guess it has to do with using like as a field name since like is a sql keyword.
i think
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.
Don't use INSERTs
use 'drupal_write_record' instead
http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_wr...