Search code examples
phpmysqlajaxdrupaldrupal-7

drupal 7 module wont insert anything to Mysql


Today i am struggling with my custom module. The problem now is that when i call page callback, where i have db_insert query, it will return me internall 500 (with message: sql state 4200, there is something wrong with the query). so javascript is calling that page witch will launch module, and in further developing it is going to give parameters, as in what will it insert into MySQL. Here is da hook:

array_push($values, array(
    'who' => 'a',
    'when' => 'a',
    'where' => 'a',
     )
);
$testi = array(
    'who' => 'me', 
    'when' => 'yesterday', 
    'where' => 'yomama',
    );
$query = db_insert('tablename')
    ->fields( $testi )
    ->execute();

print_r($testi);

I've been testing all kinda "formats" of query. For example:

$query = db_insert('pgstats')
    ->fields( array(
    'who' => 'me', 
    'when' => 'yesterday', 
    'where' => 'yomama',
    ))
    ->execute();

But nothing...

EDIT: @2pha suggested to give this information:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'when, where) VALUES ('me', 'yesterday', 'yomama')' at line 1: INSERT INTO {pgstats} (who, when, where) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => me [:db_insert_placeholder_1] => yesterday [:db_insert_placeholder_2] => yomama ) in stats_recieve_info() (line 53 of module file)


Solution

  • I found reason why it didn't work! My table has columns called "where" and "when" and for some reason, drupal didn't pass those as column names, but as syntacxes. One of those days....