tags:

views:

66

answers:

4

I'm trying to retrieve user data form a form and save the data into a database. I'm trying to retrieve the following data: 1) First Name 2) Last Name 3) Major 4) Year

SQL syntax:

CREATE TABLE `tblStudents` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(30) NOT NULL,
  `last_name` varchar(50) NOT NULL,
  `major` varchar(40) NOT NULL,
  `year` date NOT NULL,
  PRIMARY KEY (`id`)
)

I am able to save all but the Year in the database. Right now, the year that is selected is not saved in the database. It looks like this: 0000-00-00.

In the form, a user selects the year from a dropdown menu.

 <select name="year" id="year">
        <option value="2010">2010-06-12</option>
        <option value="2011">2011-06-12</option>
        <option value="2012">2012-06-12</option>
        <option value="2013">2013-06-12</option>
        <option value="2014">2014-06-12</option>

   </select>

The data type for the 'Year' column is date. Am I required to specify something else in order for the date to be saved in the database.

A: 

It may depend on the specific database in use, but typically you specify the date as text, such as '2005-10-15'. So, update mytable set somedate = '2010-10-15' will do the trick.

GrandmasterB
+1  A: 

I changed the values in the option tag from "2010" to "2010-06-12", "2011-06-12", 2012-06-12", "2013-06-12"....etc...etc (year-month-day) and was able to save those values to the database.

<select name="year" id="year">
        <option value="2010-06-12">2010-06-12</option>
        <option value="2011-06-12">2011-06-12</option>
        <option value="2012-06-12">2012-06-12</option>
        <option value="2013-06-12">2013-06-12</option>
        <option value="2014-06-12">2014-06-12</option>

   </select>
jc70
Oh. So nevermind, then. This answer would be more useful if it explained WHY that change fixed the problem... and what the actual problem *was*. ...For that matter, your solution appears to insert the same year (2010) regardless of what date is selected. Unlikely that that's correct behaviour.
djacobson
If you want to save a complete date with month and day, than your solution is just fine. If you only need the year and the month and day are not important (or always the same) make the 'year' column from type YEAR as I mentioned.
Kau-Boy
@Kau-Boy Thanks for the advice. yeah, all the dates are the same. Was trying to just see if I can be able to save a Year-Month-Day into the database. If I can do it for one, then I should be able to do it for the rest--so I just copied and pasted the Year-Month-Day, but only changed the Year to save time. But now, that I know one works... :) I'll fix the dates. But thanks again.
jc70
A: 

If you only want to save the year you can take the column type YEAR (or you can take SMALL(4)).

Kau-Boy
@Kau-Boy thanks for the tip. i'll keep that in mind, if i just want to save the year.
jc70
A: 

If you only need the YEAR, I don't agree with your last answer of adding a vague month and year.

What I will recommend is to update your table structure to update the column type to YEAR

Garis Suero