0

I am looking for something really simple(?). I am using WordPress as a CMS. I created a new page in which I added a form (questionnaire). Inside WordPress' database (the same database in which all WordPress data are), I created a new table called "ExampleTable".

Now I have 2 questions:

1) How can I insert data from the form into that custom table? 2) Is is "okay" to add a custom table (which has nothing to do with WP inside WordPress' database, or should I create a new database?)

Thanks :)

1 Answer 1

0

It is OK. And you can access this table with standard way, throught wpdb

To insert data to your table use this code:

global $wpdb;
$wpdb->insert( 
    'exampleTable', 
    array( 
        'column1' => 'value1', 
        'column2' => 123 
    ), 
    array( 
        '%s', 
        '%d' 
    ) 
);

To breafly explain what this code mean. WordPress sanitizes values before inserting them into database (to prevent database being compromited by SQL injection.). First line defines table to inser value. Secon array contains col names and value being inserted. The third array contains type of value to insers. %s stands for string, %d stands for int (digit).

You can read more about $wpdb->prepare in the codex. It is well explained in there.

2
  • Hey thanks for the answer. Sorry, I am a real beginner. Could you please explaine what %s and %d stands for; and why I need to specify it? Commented Jan 31, 2013 at 20:13
  • I've updated the answer. But have a look to the codex, everything is well explained in there: codex.wordpress.org/Class_Reference/wpdb Commented Jan 31, 2013 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.