I am trying to use the checkdate() function but when I enter a date thats not valid it is always showing as valid.
eg. When I typed the date as, 04-77-2009 the code below will return:
------------------------------
day: 04 month: 77 year: 2004
bool(true)
Successfully added CD
Return to Administrator page
-------------------------------
Now obviously the month is incorrect, it always returns true no matter what day,date or year I put. It will only return false if I put something like 'hdakdjk' in the date field. Below is my code, please let me know if you see anything im doing wroing.
Code: Select all
<?php
$DBConnect=@mysqli_connect("localhost", "localhost", "password", "database")
Or die("<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysqli_connect_errno($DBConnect) . ":" . mysqli_connect_error($DBConnect)) . "</p>";
$TableName="cd";
$date = explode("-", $_POST[DateArrived]);
echo "day: $date[0]"; // day
echo "month: $date[1]"; // month
echo "year: $date[2]"; //year
$day = date( 'd', $date[0] );
$month = date( 'm', $date[1] );
$year = date( 'Y', $date[2] );
var_dump(checkdate($month,$day,$year));
if (!$datecorrect = checkdate($month,$day,$year))
{
echo "INCORRECT DATE";
}
else{
$SQLstring = "INSERT INTO $TableName VALUES ('','$_POST[CdType]','$_POST[Artist]','$_POST[AlbumName]',STR_TO_DATE('$_POST[DateArrived]', '%d-%m-%Y'),'$_POST[CdPrice]')";
$QueryResult=@mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to add CD, please contact the web administrator</p>" . "<p>Error code " . mysqli_connect_errno($DBConnect) . ":" . mysqli_connect_error($DBConnect)) . "</p>" .
'<a href="admin.html">Return to insert CD page</a>';
echo "<p>Successfully added CD</p>";
}
echo '<a href="admin.html">Return to Administrator page</a>';
mysqli_close($DBConnect);
?>