Dashboard Single Page Form saves to DB Table

Permalink
Ok. I've been all over the forums ad docs and still can't find a decent example of saving data to a db table via a single_page. I know it's possible but I'm struggling big time getting it to work. If anyone has a sample view and controller I would be much obliged. I'll post mine here if I ever figure out what I'm doing. Thanks

 
zenmastercraig replied on at Permalink Reply
zenmastercraig
Did you ever find an answer to this? I'm looking for the same thing.

I know I can 'roll my own' using something like:

Function call within add():
$this->saveFormData($this->post());


Function code:
private function saveFormData($post){
$query = "INSERT INTO NewsArticle
(title, summary)
VALUES
('{$post['title']}', '{$post['summary']}') ";
$db = Loader::db();
$db->Execute($query);
$id = $db->Insert_ID();
return $id;
}


Note: add() is called within a dashboard controller.

This approach lacks string escape (someone is bound to put an apostrophe somewhere), and INSERT/UPDATE logic for added records vs. edited records.

I can build all of that into my save function, but it seems like C5 has a native function/method for just about everything I've ever thought of. It's a shame to have to spend extra coding time because I can't easily find the documentation.
jordanlev replied on at Permalink Reply
jordanlev
re: escaping -- the ADODB library (which C5 uses) has built-in escaping via parameterized queries. You should *ALWAYS* run your queries this way instead:
$query = "INSERT INTO NewsArticle (title, summary) VALUES (?, ?)";
$vals = array($this->post('title'), $this->post('summary'));
$db = Loader::db();
$db->Execute($query, $vals);
//etc. etc.
jordanlev replied on at Permalink Reply
jordanlev
This is a really great walk-through of all the things involved:
http://www.concrete5.org/documentation/how-tos/developers/build-a-s...

For the part specifically about saving to a database table, scroll down to the yellow "Add/Update" heading under the "Controller Code" section.