php/mysql question
Page 1 of 1
VGAdeadcafe




Posts: 22230
Location: ★ ಠ_ಠ ★
PostPosted: Sat, 9th Jun 2012 16:46    Post subject: php/mysql question
Hello guys, I'm making a little "business planner" thingy online. Each user can fill in a template-thingy form and store it.

The thing is, the form has many elements and it's a chore to store (mysql) each manually. Is there some kind of way to store all that chunk of info (textboxes mainly) automagically in the DB ?
Back to top
[mrt]
[Admin] Code Monkey



Posts: 1342

PostPosted: Sat, 9th Jun 2012 23:07    Post subject:
Well there's always a sacrifice of manual labour vs being able to index data clearly. For one, if you absolutley have no use of each individual field you can simply create a array of all the fields then serialize it in php before you store it as one huge magic blob in mysql. The downside is that mysql wont be able to tell the difference and you wont be able to query on the data.

The other way is to write a script to automatically map inputs to mysql fields and generate a query based on it. Thats what the MP40 dudes would call semi-automatic mode Very Happy

Then there's always the dreaded hands-on approach. The good news is that you'll only have to write that code once if you plan it right.

Your choice!


teey
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Sat, 9th Jun 2012 23:54    Post subject:
What you want/need is ORM - Object Relational Mapping. Doctrine is probably the most common one in PHP right now, may want to give that a try.

Basically what it means is that you create a PHP object like below, and somewhere it has a map of which field on the object is which field in the DB. You then only have to match each field from the POST data to the appropriate field in the object, which Doctrine has a simple utility method for.

Code:
class Something {
  public $id;
  public $title;
  public $something;
}

$something = new Something();
$something->fromArray($_POST);
$something->save();


The benefit to using something like Doctrine is that you can also do the validation within the object by using setters; so for the title you'd have a method setTitle($value); and within that function you can check whether it meets requirements etcetera Smile
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group