Drupal 6 SQL insert FAQ: Can you share an example of how to write a Drupal 6 SQL insert statement?
There are at least two ways to write a SQL insert statement in Drupal 6, and I'll demonstrate both of those here.
A Drupal 6 SQL INSERT example using db_query
The first method involves writing an old-fashioned SQL INSERT statement with the Drupal 6 db_query function, like this:
# do the database insert db_query("INSERT INTO {reminders} (uid, event, event_time) VALUES (%d, '%s', '%s')", $uid, $event, $datetime);
In this case I have a Drupal database table named 'reminders', and I'm inserting those three variables into the table.
Drupal 6 SQL INSERT using drupal_write_record
I'm not a Drupal expert by any stretch, and from what I just read this second Drupal 6 insert syntax appears to be favored over writing your own SQL:
# another way to do the database insert $data = array( 'uid' => $uid, 'event' => $event, 'event_time' => $datetime ); drupal_write_record('minime_reminders', $data);
I'll try to read more about this second approach and see why it's "preferred", but until then I thought I'd share both ways to perform a Drupal 6 SQL insert.
Please note that if you're using Drupal 7, I know for sure that this SQL insert approach has changed, but I'm not ready to tackle that subject just yet.