Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form value text to date

Status
Not open for further replies.

matrixindicator

IS-IT--Management
Sep 6, 2007
418
BE
I have a formfield where I insert a date 2009-12-23, the day of today. The user can if he want change the date manually in the field. He submit this data and it should be inserted in the database.

For the date he insert nothing 0000-00-00 I checked the value of the form filed passed and its ok. I suppose it has to do something with data type ? In the test I set NOW() functionin the insert sql, this works. Is strtotime function usefull ?

Code:
	$today = $_POST['fdatum'];
	$todayinsert = strtotime('Y-m-d', $today);
	echo $todayinsert;
 
Hi
[ul]
[li]What exactly entered the user ?[/li]
[li]What that [tt]echo [navy]$todayinsert[/navy][teal];[/teal][/tt] said ?[/li]
[li]What kind of database are you using ?[/li]
[/ul]


Feherke.
 
The user has the possibility to change or not the presented date (now()).
I use MySql.

I did few tests.
1. '2009-12-12' in the insert query = OK
2. NOW() function insert query = OK
3. echo the value $todayinert 2009-12-24 = OK
4. $today = $_POST['fdatum'];
$time = strtotime($today);
$myDate = date( 'Y-m-d', $time );
converting text tot date = NOK

after inserting the data I always got
0000-00-00 the default value. Field is a date type.



 
Code:
$sql = "INSERT INTO tvme04_tank(Liter, LDatum, refIDeig, LReg) VALUES($liters, $myDate, $ideig, NOW())";
 
i would suggest enquoting the value of $myDate for all values that are not unix date times being inserted into an integer or other numeric field.

for example
Code:
2009-12-22

is treated as a string because it contains non-numeric characters. string literals must be enquoted before use in a sql query.
 
in other words :)

$sql = "INSERT INTO tvme04_tank(Liter, LDatum, refIDeig, LReg) VALUES('" . $liters . "', '" . $myDate . "', '" . $ideig . "', NOW())";

hope it helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top