never_had_a_name
never_had_a_name

Reputation: 93246

where to store information like gender and year of birth?

i have users and i need them to specify a gender (male, female) and year of birth (1930, 1931...1999, 2000).

i wonder where i should store these values:

  1. in the database?
  2. in php file?

if i store them in the database i have to manually create all entries first. but a good thing is that the user table will have constraints so the gender field will always be male or female, it cannot be something else.

if i store them in the php file (eg. as html) then i can easily add/remove values. but a con is that i dont have the constraints in database, so another value could be stored as gender by mistake, even though i could add validation in php backend so even if someone hacked the html it is not stored unless it's either male or female.

what is best practice to do this?

thanks

Upvotes: 1

Views: 553

Answers (5)

David Morrow
David Morrow

Reputation: 9354

use a database, easy call...

Navicat is the bomb when it comes to a gui if you are looking for that sort of deal.

Upvotes: 1

Olly Hicks
Olly Hicks

Reputation: 1104

What format is your data in now? Chances are you will be able to automatically insert it into the database. Should be some good classes on phpclasses.org that will help. Would defiantly be worth installing phpMyAdmin on your server (or you might already have it). Makes working with databases so much easier.

Upvotes: 2

Mahomedalid
Mahomedalid

Reputation: 3124

If I understand right you need to store the 1997, 1998, 1999, etc... 'Male', 'Female' values, NOT the complete user row. If is in that way I think the best solution is store it in a external file, like ini, xml, php (array), etc...

A excelent choice is generate that file from the database metadata. For example, if you have a enum field ENUM('Male', 'Female') then you generate the options in that way. Actually you can generate the default input size, number or date validators, etc...

I think the best solution is separate the backend datastore from the view. What if you need to translate the page so you need to show 'Masculino' instead of 'Male' but remains in the database the value 'Male'?

Upvotes: 0

Nick Vallely
Nick Vallely

Reputation: 1396

I would definitely go with a database, as that is the main point of a database, to house data. You can, as you said add restraints, expand collected data (i.e. addresses, phone numbers, user likes/dislikes), and search/query for data much easier with a database. As for the initial entry, there are many ways of getting data into a database with sql. There may be a very simple way of getting your current users into the database without too much work.

Upvotes: 1

Alistair
Alistair

Reputation: 1326

Go with a database as you'll no doubt want to query records later and working with HTML (or text files) for this type of thing would be a pain. As Dave said a little upfront work saves your bacon later!

Upvotes: 1

Related Questions