Date Problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Date Problems

Post by ashrafzia »

I want to insert current date and due date for the invoices in the database.
I have tried alot but didn't get any fruitful result.
I am providing the user with due_date field and the current date should insert automatically with the record.
The datatypes of the current_date and the due_date both are DATE.
I want to clear one more thing, should i specify the date format to the user to use?

Code: Select all

$c_date=date("Y-m-d");
$d_date='$_post[due_date]'; //just putting value from text box
 
$sql= "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}',
    service_description='{$_POST['s_desc']}', comments='{$_POST['comments']}', init_price='{$_POST['i_price']}', 
    price='{$_POST['price']}', adjustments='{$_POST['adj']}', total_price='{$_POST['t_price']}', 
    current_date='$date' , due_date='{$_POST['d_date']}' "; 
I don't know what to write for the due_date in the query.

Please help.
Last edited by ashrafzia on Wed May 14, 2008 7:45 am, edited 1 time in total.
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: Date Problems

Post by lettie_dude »

Hi

Yes you need to make sure the format from the due date variable is the same as the DATE fromat in mysql i.e. 0000-00-00 otherwise it will not work. You could use dropdowns to enter the date i.e. one for day one for month one for year so the processing code will always receive a relevant number for each 2008-12-01 etc
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Re: Date Problems

Post by ashrafzia »

lettie_dude wrote:Hi

Yes you need to make sure the format from the due date variable is the same as the DATE fromat in mysql i.e. 0000-00-00 otherwise it will not work. You could use dropdowns to enter the date i.e. one for day one for month one for year so the processing code will always receive a relevant number for each 2008-12-01 etc
Okay. I have got three list boxes with Year, Month and Day.
How should i build my query ?
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: Date Problems

Post by lettie_dude »

You need and should first check the input data to make sure it is what you are expecting for security purposes.

Create variables for each input once you have checked it and then create a final d_date variable adding in the dashes see example:

Code: Select all

$day = $_POST['day'];
$month =  $_POST['month'];
$year =  $_POST['year'];
 
$d_date = $year.'-'.$month.'-'.$day;
Please note the above example does not include any data cleaning which you should always use for form input regardless.

Hope this helps
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Date Problems

Post by aceconcepts »

You're writing you query incorrectly:

Yopu are writing it like an UPDATE query

Code: Select all

 
$sql= "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}',
    service_description='{$_POST['s_desc']}', comments='{$_POST['comments']}', init_price='{$_POST['i_price']}',
    price='{$_POST['price']}', adjustments='{$_POST['adj']}', total_price='{$_POST['t_price']}',
    current_date='$date' , due_date='{$_POST['d_date']}' "; 
 
It should take the following form:

Code: Select all

 
$sql= "INSERT INTO invoices(PUT YOUR COLUMN NAMES HERE) 
    VALUES('$_POST['c_id']', '$_POST['s_name']','$_POST['s_desc']', '$_POST['comments']', '$_POST['i_price']', '$_POST['price']', '$_POST['adj']', '$_POST['t_price']', '$date' , '$_POST['d_date']') "; 
 
Post Reply