Page 1 of 1

varchar and date help pls...

Posted: Tue Jul 12, 2005 6:57 am
by pleigh
so far i have this code for date

Code: Select all

<?
if (isset($_POST['submit']))
{
	$message = NULL;
	
	if (empty($_POST['month']) || empty($_POST['day']) || empty($_POST['year']))
    {
        $sd = false;
        $message .= 'no date<br>';
    }
    else
    {
        $sd = $_POST['month']."-".$_POST['day']."-".$_POST['year'];
    } 
	
	
	if ($sd)
	{		
		$query = "INSERT INTO sample(startdate) VALUES ('$sd')";
		$result = @mysql_query($query) or die(mysql_error());
		
		if ($result)
		{
			echo 'written to db';
			exit();
		}
		else
		{
			$message .= 'no data written in the db';
			
		}
		mysql_close();
	}
	else
	{
		$message .= 'please try again';
	}
}

if (isset($message))
{
	echo '<font color=red>'.$message.'</font>';
}
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
  
  
  <? calendar(); ?>
<input type="submit" name="submit" value="submit" />
</form>
the code above works and is written to mysql as varchar...but changing the startdate properties to date will not write the right value of the form to mysql...i tried to click the submit button with the desired date, will prompt me that the process is successful...but the value written to my database is 0000-00-00 and not the date i supplied

Posted: Tue Jul 12, 2005 6:58 am
by timvw
Change

Code: Select all

$sd = $_POST['month']."-".$_POST['day']."-".$_POST['year'];
to

Code: Select all

$sd = $_POST['year'] . "-" . $_POST['month']."-".$_POST['day'];

(Btw, you might want to perform data validation....)

Posted: Tue Jul 12, 2005 7:02 am
by pleigh
hi tim...thanks, it worked...:D

but i can't understand you point here
(Btw, you might want to perform data validation....)