473,748 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to fix the date in a MySQL db

Hello,

In my MySQL database, one of the fields eople have to fill in is "DOB"
(Date of Birth).
I have now managed to begin inserting data into my DB via a form, the
data type for the DOB field is "DATE", but every time I add data and
then check my db, I see the DOB displayed as "0000-00-00".
Has anyone got any idea how I could put this right?

TIA,
Newsscanner.
Aug 16 '05 #1
10 2371
Newsscanner wrote:
I have now managed to begin inserting data into my DB via a form, the
data type for the DOB field is "DATE", but every time I add data and
then check my db, I see the DOB displayed as "0000-00-00".
Has anyone got any idea how I could put this right?


Can you provide the SQL query you are using to insert the data? Without
it, it is impossible to tell what you are doing wrong, but commong
mistakes are:

# Sometimes user forgets to add '' around the value
insert into table(DOB) values( 2005-08-16 ); # Wrong
insert into table(DOB) values( '2005-08-16' ); # Correct

# Sometimes the date is given in wrong format. Use yyyy-mm-dd format
insert into table(DOB) values( '8/16/2005' ); # Wrong
insert into table(DOB) values( '2005-08-16' ); # Correct

# Comming error also happens with the programming language
# and the value of the variable is empty
insert into table(DOB) values( '' ); # Wrong
insert into table(DOB) values( '2005-08-16' ); # Correct
Aug 16 '05 #2
Aggro wrote:
Newsscanner wrote:
I have now managed to begin inserting data into my DB via a form, the
data type for the DOB field is "DATE", but every time I add data and
then check my db, I see the DOB displayed as "0000-00-00".
Has anyone got any idea how I could put this right?

Can you provide the SQL query you are using to insert the data? Without
it, it is impossible to tell what you are doing wrong, but commong
mistakes are:

# Sometimes user forgets to add '' around the value
insert into table(DOB) values( 2005-08-16 ); # Wrong
insert into table(DOB) values( '2005-08-16' ); # Correct

# Sometimes the date is given in wrong format. Use yyyy-mm-dd format
insert into table(DOB) values( '8/16/2005' ); # Wrong
insert into table(DOB) values( '2005-08-16' ); # Correct

# Comming error also happens with the programming language
# and the value of the variable is empty
insert into table(DOB) values( '' ); # Wrong
insert into table(DOB) values( '2005-08-16' ); # Correct

OK, thanks.
But since the input comes from the user, I thought it would display
whatever has been typed in.
My query looks like this:
"$DOB=$_POS T['DOB'];
And further:
"$query = "INSERT into Names (Name,FirstName s,DOB,Sex)
VALUES ('$Name','$Firs tNames','$DOB', '$Sex')";"

So, is that where I do something wrong?

TIA,
Newsscanner.
Aug 16 '05 #3
Newsscanner wrote:
"$query = "INSERT into Names (Name,FirstName s,DOB,Sex)
VALUES ('$Name','$Firs tNames','$DOB', '$Sex')";"


That is not the query. That is PHP code which generates the query.

echo $query;

And tell what it prints out.
Aug 16 '05 #4
Newsscanner wrote:
But since the input comes from the user, I thought it would display
whatever has been typed in.
My query looks like this:
"$DOB=$_POS T['DOB'];
And further:
"$query = "INSERT into Names (Name,FirstName s,DOB,Sex)
VALUES ('$Name','$Firs tNames','$DOB', '$Sex')";"


That will work only if your DOB field is a CHAR or VARCHAR.
MySQL needs to know how to parse the DOB string into a real date value.
MySQL understands _one_ date input format: YYYY-MM-DD, with optional
HH::MM::SS.

MySQL also has a STR_TO_DATE() function, but again, each invocation of
that function understands one format, which you define in an argument to
the function. See
http://dev.mysql.com/doc/mysql/en/da...functions.html.

In one project, I had to allow users to type in an arbitrary string in a
Perl web application, so I used the Perl module Date::Manip, which has a
very flexible date-parsing feature (it even parses correctly strings
like "a week from next tuesday"). Then I'd verify that the date parsing
succeeded, or else return an error to the user and ask them to try
again. If it succeeds, then transform the date into YYYY-MM-DD and use
that for the SQL statement.

I'm guessing you're using PHP based on the use of $_POST['DOB'].
I'm not as familiar with PHP as I am with Perl, but I see in the docs
that there's a function strtotime() that does some pretty flexible
date-parsing. http://us3.php.net/strtotime

Also, it's very important that you validate all user data before using
them in your SQL statements. What if some malicious user posts the
following value for DOB and you interpolate it into your query above
without any validation:

2000-01-01', 'M'); DELETE from Names; // Mwa ha ha

Regards,
Bill K.
Aug 16 '05 #5
Aggro wrote:
Newsscanner wrote:
"$query = "INSERT into Names (Name,FirstName s,DOB,Sex)
VALUES ('$Name','$Firs tNames','$DOB', '$Sex')";"

That is not the query. That is PHP code which generates the query.

echo $query;

And tell what it prints out.

After doing that and entering some dummy data (also entering the date in
MySQL's own format YYYY-mm-DD), this is what I got:
"INSERT into Names (Name,FirstName s,DOB,Sex) VALUES
('Ok','gie','20 01-01-01','Male')"

Thanks,
Newsscanner.
Aug 16 '05 #6
Bill Karwin wrote:
Newsscanner wrote:
But since the input comes from the user, I thought it would display
whatever has been typed in.
My query looks like this:
"$DOB=$_POS T['DOB'];
And further:
"$query = "INSERT into Names (Name,FirstName s,DOB,Sex)
VALUES ('$Name','$Firs tNames','$DOB', '$Sex')";"

That will work only if your DOB field is a CHAR or VARCHAR.
MySQL needs to know how to parse the DOB string into a real date value.
MySQL understands _one_ date input format: YYYY-MM-DD, with optional
HH::MM::SS.

MySQL also has a STR_TO_DATE() function, but again, each invocation of
that function understands one format, which you define in an argument to
the function. See
http://dev.mysql.com/doc/mysql/en/da...functions.html.

In one project, I had to allow users to type in an arbitrary string in a
Perl web application, so I used the Perl module Date::Manip, which has a
very flexible date-parsing feature (it even parses correctly strings
like "a week from next tuesday"). Then I'd verify that the date parsing
succeeded, or else return an error to the user and ask them to try
again. If it succeeds, then transform the date into YYYY-MM-DD and use
that for the SQL statement.

I'm guessing you're using PHP based on the use of $_POST['DOB'].
I'm not as familiar with PHP as I am with Perl, but I see in the docs
that there's a function strtotime() that does some pretty flexible
date-parsing. http://us3.php.net/strtotime

Also, it's very important that you validate all user data before using
them in your SQL statements. What if some malicious user posts the
following value for DOB and you interpolate it into your query above
without any validation:

2000-01-01', 'M'); DELETE from Names; // Mwa ha ha

Regards,
Bill K.

Thanks Bill.
Which also reminds me of another problem (I am a newbie, remember).
The first and primary field is to give everyone an ID number, it is
generated automatically.
Here is the setup:
ID tinyint(4) UNSIGNED No auto_increment
Change Drop Primary Index Unique Fulltext

What puzzles me is that, since I started entering dummy data for testing
purposes, and then erased them, my ID keeps incrementing, in other
words, it does not seem to be reset when the DB gets emptied.
Anything I can do about this?

Thx,
Newsscanner.
Aug 16 '05 #7
Newsscanner wrote:
What puzzles me is that, since I started entering dummy data for testing
purposes, and then erased them, my ID keeps incrementing, in other
words, it does not seem to be reset when the DB gets emptied.
Anything I can do about this?


Auto-increment values are supposed to be unique, but are not necessarily
contiguous, and are only monotonically increasing by coincidence of the
implementation.

Consider what would happen if you were inserting non-dummy data for some
number of weeks or months. Say there'd now be 75,000 records in that table.

Then one day you need to delete the record with primary key value 34,221
for some legitimate reason.

Should MySQL renumber the primary keys of all subsequent rows
34,222-75,000 to keep the primary key values contiguous? What if there
were millions of rows following the one you want to delete? What about
foreign key values in tables that reference this primary table? All
those rows that reference the thousands or millions of rows whose
primary key values are changing would also have to change.

If MySQL were to keep auto-generated primary key values contiguous, it
would almost certainly make a simple single-row DELETE operation take an
unreasonably long time.

Regards,
Bill K.
Aug 16 '05 #8
Newsscanner wrote:
After doing that and entering some dummy data (also entering the date in
MySQL's own format YYYY-mm-DD), this is what I got:
"INSERT into Names (Name,FirstName s,DOB,Sex) VALUES
('Ok','gie','20 01-01-01','Male')"


Did this value insert correctly, or is it '0000-00-00' when you retrieve
it from the database?

What is the datatype of the Names.DOB field?

Can you also tell us what version of MySQL you are using? Some
date/time datatypes have changed behavior subtlely between versions.

Regards,
Bill K.
Aug 16 '05 #9
Bill Karwin wrote:
Newsscanner wrote:
After doing that and entering some dummy data (also entering the date
in MySQL's own format YYYY-mm-DD), this is what I got:
"INSERT into Names (Name,FirstName s,DOB,Sex) VALUES
('Ok','gie','20 01-01-01','Male')"

Did this value insert correctly, or is it '0000-00-00' when you retrieve
it from the database?

What is the datatype of the Names.DOB field?

Can you also tell us what version of MySQL you are using? Some
date/time datatypes have changed behavior subtlely between versions.

Regards,
Bill K.

It did insert correctly, but I literally inserted "2001-01-01".
The datatype is "DATE".
I have MySQL 4.1.13
Thanks.
Aug 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3188
by: Yulia Yegenov | last post by:
I have a query that looks like this: (I insert the date created with the php date function) $status = "Active"; //(I cannot use the mysql timestamp function for other reason). $curdate = date("m/j/Y H:i"); $owner = "not assigned"; @ $db_connect = mysql_connect("localhost", "$db_username", "$db_password");
5
3734
by: Dominique Javet | last post by:
Hello, I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this "newbie" question... I've found no answer in the forum ... I've a date problem with my formular. In my mysql DB my filed "date" in table "experience" is like this: Y-m-d (2002-07-23). My fied`date` is date, NOT NULL with no default entry My form read well the date data depending the id, (pe. 30.02.2003), but when I submit a new date, I receive as result...
0
1841
by: Jack Lauman | last post by:
I have a table with a column "date" that contains dates in SQL format, i.e. 2003-08-10 I need to query the table and return all the rows from the current date thru the next 6 days. (i.e. today 2003-08-10 thru Saturday 2003-08-16). I have tried the following query which returns all of the desired rows except those for the current date. How can I correct this? SELECT date, time, am_pm, tz, height, cond FROM cherry_point_tides WHERE
4
26366
by: MyOracle | last post by:
Hi everybody, I just curious about change date(0000-00-00) to date (00-00-0000) in mysql.Can anyone tell me about that. Thanks. izmanhaidi.
2
17389
by: Paul | last post by:
I am trying to insert a date into a mysql table but everytime I attempt it the date field is always empty. The field is of Date type and the date in code is in European format dd/mm/yyyy. Does the date have to be formatted in any particualr way from vb.net.
3
3442
by: dave | last post by:
I need to compute an expiration date based on the number of hours, days, or months purchased. The expiration date needs to be expressed in minutes something like '1260481600'. How can I get the current date and time expressed in minutes? Once I have this number I can add the number of minutes purchased to the current date/time to get the expiration date.
6
3934
by: Geoff Cox | last post by:
Hello, at the moment I can add the combined date and time into MySQL using php $dt1 = date("Y-m-d H:i:s"); is it possible to add the date and time separately? I thought it might be
10
4023
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one problem. This function does not recognize time zones. How to adjust date to user's time zone? Is converting to timestamp() and then to format readable to visitors the one and only solution (e.g. strtotime() + date() OR DateTime object)? Perhaps,...
3
3541
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im sorting out a few glitches though. Since i upgraded from ms access database to MySQL, i have added about 4 articles to test the new setup. I note some fields aren't being added in the new mySql database for the new 4 records. When i ran the MySQK...
0
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.