Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Inserting date into db
Old 03-18-2010, 11:14 PM Inserting date into db
Extreme Talker

Posts: 168
Trades: 0
I am trying to insert the following line into mySQL. I then received the following error.

Quote:
INSERT failed: INSERT INTO tbl_md4c (project, level, part, part_desc, status, edate, rdate, qty, pr, po, line, factory, error, prod) VALUES( '60227648 ','3','500012A-042','VALVE-CHK,SWING,SXS,316-PTFE,0.50,KITZ ', 'SubctctReq','','09/11/02', '1','','','', '3899','','')
Incorrect date value: '' for column 'edate' at row 1
The problem appear to be that the value associated with the "edate" is empty. I have set up the db as a date type value with a default NULL value.

Not sure why it will not accept the data or the lack there of as the case might be?
dgkindy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-19-2010, 09:37 PM Re: Inserting date into db
Super Talker

Posts: 101
Trades: 0
Never mind my earlier post (now deleted), I mistook the commas in 4th value to mean multiple ones. So I thought you had unequal columns to values.

Last edited by Towhid; 03-19-2010 at 09:53 PM..
Towhid is offline
Reply With Quote
View Public Profile
 
Old 03-20-2010, 12:32 PM Re: Inserting date into db
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Are you sure that it's not your rdate column that is set to allow null?

Can you show us the structure of your table?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-20-2010, 08:30 PM Re: Inserting date into db
Extreme Talker

Posts: 168
Trades: 0
Here is a screen shot of the structure for the table.

I checked it and NULL is displayed as the default value.
Attached Images
File Type: png Untitled-1.png (13.6 KB, 5 views)
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 03-20-2010, 09:41 PM Re: Inserting date into db
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ok, I see it now...
In the field "edate", you try to insert the date '', which is invalid.

An empty string is not a null.
A null is represented by the keyword "null".

So, your query must be written:
Code:
INSERT INTO tbl_md4c (project, level, part, part_desc, status, edate, rdate, qty, pr, po, line, factory, error, prod) 
VALUES( '60227648 ','3','500012A-042','VALVE-CHK,SWING,SXS,316-PTFE,0.50,KITZ ', 'SubctctReq',NULL,'09/11/02', '1','','','', '3899','','')
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-20-2010, 10:39 PM Re: Inserting date into db
Extreme Talker

Posts: 168
Trades: 0
So if I removed the default NULL from the table and set it to none, should it then accept ""? I tried this but it still did not work but want to make sure I understand this?
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 03-20-2010, 10:42 PM Re: Inserting date into db
Extreme Talker

Posts: 168
Trades: 0
I guess I am also a bit confused as the PO and LINE are set to NULL yet I have records that have been accepted without a problem. Is it a case that DATE does not accept "" as a value?
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 03-21-2010, 09:23 AM Re: Inserting date into db
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
So if I removed the default NULL from the table and set it to none, should it then accept ""? I tried this but it still did not work but want to make sure I understand this?
No.
You have not fully understood what a null is.

Basically, you could translate "null" by "unknown".
An empty string is not null, it's empty.

The problem is that your column is typed as a date/time column. So, you can only insert a date in it.
When you give mysql '09/11/02', the string is converted automatically in a date.
But when you give it '', then mysql cannot convert this to a date, thus the error.

If you don't want to have a value in the field, you must tell explicitly mysql so, by using the null keyword.

Quote:
Is it a case that DATE does not accept "" as a value?
It's exactly that!

That the reason too, why
Code:
select NULL+3
gives a result of NULL.
An unknown value+3 is unknown.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-21-2010, 09:54 AM Re: Inserting date into db
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by dgkindy View Post
So if I removed the default NULL from the table and set it to none, should it then accept ""? I tried this but it still did not work but want to make sure I understand this?
Or remove the column and the associated value from the insert UNLESS there is a valid date to insert.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-21-2010, 09:57 PM Re: Inserting date into db
Extreme Talker

Posts: 168
Trades: 0
Okay, based on your commentary, I did a bit more investigation. This is what I found.

When I create the variable, it is created with a NULL value.
PHP Code:
class Material  {
        public 
$level NULL;       //Part level
        
public $part NULL;            //Part number
        
public $part_desc NULL;       //Part description
        
public $status "Blank";    //Status of part
        
public $edate NULL;           //Date the parts are expected
        
public $rdate NULL;           //Date the parts are required by
        
public $qty NULL;             //Required quantity for project
        
public $pr NULL;              //Purchase requisition number to buy part 
        
public $po NULL;              //PO number that is buying the part
        
public $line NULL;            //Line on PO that is purchasing the part
        
public $factory NULL;         //Manufacturing facility for part
        
public $error NULL;           //SAP error code
        
public $prod NULL;            //Production order number
        

I then confirmed the value is NULL after creation
PHP Code:
           if(is_null($material[$bin]->edate)){
   echo 
'value is null <br />';
   } 
I then confirmed the value is still NULL just prior to inserting the data into the database.
PHP Code:
             if(is_null($value->edate)){
   echo 
'value is null on dump <br />';
   } 
code that is used to generate the sql statement
PHP Code:
  $query "INSERT INTO tbl_md4c (project, level, part, part_desc, status, edate, rdate, 
          qty, pr, po, line, factory, error, prod) 
          VALUES"
."(
          '
$project','$value->level','$value->part','$value->part_desc',
          '
$value->status','$value->edate','$value->rdate',
          '
$value->qty','$value->pr','$value->po','$value->line',
          '
$value->factory','$value->error','$value->prod')"
Results that are printed on the screen.
Quote:
value is null on dump
INSERT failed: INSERT INTO tbl_md4c (project, level, part, part_desc, status, edate, rdate, qty, pr, po, line, factory, error, prod) VALUES( '60227648 ','2','3037920','U-BOLT,GLV,0.25-20UNC,0.50PIPE ', 'Plant','','09/12/02', '1','','','', '3899','','')
Incorrect date value: '' for column 'edate' at row 1


So, it would seem that the value is NULL but when I go to insert the value, php is converting the NULL value to "". How do you maintain the NULL value. Understand that, out of 14 values, about 5 of them are mandatory, while the other values may or may not exist depending on the record. All variables are required.
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 03-22-2010, 05:03 AM Re: Inserting date into db
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
you need to drop the single quotes and replace the value null with the keyword "null" in that case:
PHP Code:
<?
function nullOrNot($val){
    
$ret=$val===null?"NULL":"'$val'";
    return 
$ret;
}

$query "INSERT INTO tbl_md4c (project, level, part, part_desc, status, edate, rdate, qty, pr, po, line, factory, error, prod) 
          VALUES (
          '
$project'
          ,"
.nullOrNot($value->level)."
          ,"
.nullOrNot($value->part)."
          ,"
.nullOrNot($value->part_desc)."
          ,"
.nullOrNot($value->status)."
          ,"
.nullOrNot($value->edate)."
          ,"
.nullOrNot($value->rdate)."
          ,"
.nullOrNot($value->qty)."
          ,"
.nullOrNot($value->pr)."
          ,"
.nullOrNot($value->po)."
          ,"
.nullOrNot($value->line)."
          ,"
.nullOrNot($value->factory)."
          ,"
.nullOrNot($value->error)."
          ,"
.nullOrNot($value->prod).")";
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Inserting date into db
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.05983 seconds with 13 queries