Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 :)

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
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? –  Kaneda Jan 31 '13 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 –  david.binda Jan 31 '13 at 20:42
    
Thanks for the fast and supportive comment :) –  Kaneda Jan 31 '13 at 21:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.