Transfer data from a form to a database table

Tobias Hassebrock Posted in Beginning Developers 6 years ago

hey,

I want to send the data which user can input in my new form in my new component to a new database table. I created in the following table in the file "opensource-socialnetwork.sql".

>  CREATE TABLE IF NOT EXISTS `ossn_proposals_db` (
>           `id` bigint(20) NOT NULL AUTO_INCREMENT,
>           `upload_from` bigint(20) NOT NULL,
>           `text_title` text NOT NULL,
>           `text_theme` text NOT NULL,
>           `text_level` text NOT NULL,
>           `text_organisation` text NOT NULL,
>           `text_language` text NOT NULL,
>           `text_content` text NOT NULL,
>           `time` int(11) NOT NULL,
>           PRIMARY KEY (`id`)
>         ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Then I created the following form in my comonent directory. The user should insert the title, themename, level, organisation, the language and a text. All data are textstrings.

> <div class="test">
>     <input type="text" name="title" placeholder="<?php echo ossn_print('proposal:search:title'); ?>"/>
>     <input type="text" name="theme" placeholder="<?php echo ossn_print('proposal:search:theme'); ?>"/> </div>
> 
> <div>
>     <input type="text" name="level" placeholder="<?php echo ossn_print('proposal:search:level'); ?>"/>
>     <input type="text" name="organisation" placeholder="<?php echo ossn_print('proposal:search:organisation'); ?>"/> </div>
> 
> <div>
>     <input type="text" name="language" placeholder="<?php echo ossn_print('proposal:search:language'); ?>"/> </div>
> 
> <textarea rows="4" cols="50"> <?php echo
> ossn_print('proposal:search:text'); ?> </textarea> </div>

Which function do I have to use to transfer the data to my databank? And how do I use them? Is it the following? Is there somewhere a simple data transferfile where I can learn how it is working at OSSN?

public function insert($params) {
                if(is_array($params)) {
                        if(count($params['names']) == count($params['values'])) {
                                $colums = "`" . implode("`, `", $params['names']) . '`';
                                $values = "'" . implode("', '", $params['values']) . "'";
                                $query  = "INSERT INTO {$params['into']} ($colums) VALUES ($values);";
                                $this->statement($query);
                                if($this->execute()) {
                                        return true;
                                }
                        }
                }
                return false;
        }

public function statement($query) {
                if(!empty($query)) {
                        $this->query = $query;
                        return true;
                }
                return false;
        }
Replies
Indonesian Arsalan Shah Replied 6 years ago

Hi,
Please do not invent your own tables and touch the ossn database, instead utilize ossn framework, here is the code for your table (without really creating tables in database)

https://gist.github.com/lianglee/c9f20f2da7a49e8da06b0227a0abc0d1

You can also take a look at Blog component.