varchar and date help pls...

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
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

varchar and date help pls...

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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....)
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

hi tim...thanks, it worked...:D

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