Page 1 of 1

Date Problems

Posted: Wed May 14, 2008 7:03 am
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.

Re: Date Problems

Posted: Wed May 14, 2008 7:39 am
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

Re: Date Problems

Posted: Wed May 14, 2008 7:59 am
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 ?

Re: Date Problems

Posted: Wed May 14, 2008 8:12 am
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

Re: Date Problems

Posted: Wed May 14, 2008 9:20 am
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']') ";