Saving Adhoc Data in Drupal 7

Primary tabs

If you want to play with nodes, taxomony and comments, there is a process and set of functions that you should use to do that.

If you just want to drop some adhoc data into a table, then you can use the db_insert() function.

Here is an example.

$data["field1"] = "value1";
$data["field2"] = 123.45;
etc...
$result = db_insert('tablename') -> fields($data) -> execute();

If you get it wrong, you will get a PDO Error - which will stop your site. So, you can get around this with

    try {
        $id = db_insert('tablename') -> fields($data) -> execute();
    }
    catch (Exception $e) {
        drupal_set_message(t("Sorry, that didn't work."), 'error');
    }

If you want to insert new data, but if it exists, then update what is there, use the db_merge() function.

You can also use the drupal_write_record() function in a similar fashion.

$data["field1"] = "value1";
$data["field2"] = 123.45;
etc...
$result = drupal_write_record('tablename, $data);

Tags:

Your IP Info

IP Address Locator

Speed Test

Test how fast your Internet connection really is.

DropBox

Use this link to get DropBox http://db.tt/iQHWaaF