kenjpete
asked on
Problem with inserting date into MySQL using PHP post form variable
I have an HTML form using the jquery datepicker to insert the date into a MySQL database using PHP post form variable. Everytime I try to insert the date it inserts the date as 0000-00-00. I've been looking around for the solution and I can't seem to find it. My code is below.
PHP:
$release_date=$_POST['rele ase_date'] ;
$date=strtotime($release_d ate);
HTML:
<input type="text" name="release_date" id="datepicker" size="40" required>
SQL:
$sql = "INSERT INTO books(title, description, price, release_date, quantity, active) VALUES ('$title','$description',' $price',$d ate,'$quan tity','$ac tive');";
The release_date field in my database table is set to DATE.
PHP:
$release_date=$_POST['rele
$date=strtotime($release_d
HTML:
<input type="text" name="release_date" id="datepicker" size="40" required>
SQL:
$sql = "INSERT INTO books(title, description, price, release_date, quantity, active) VALUES ('$title','$description','
The release_date field in my database table is set to DATE.
From http://php.net/manual/en/function.date.php ...
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)You can shorten it to this for just DATE.
$today = date("Y-m-d"); // 2001-03-10 (the MySQL DATE format)MySQL is very picky about the format for a DATE field. Which is why I always use dropdowns instead of allowing users to type in unformatted dates.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok a combination of things worked.....first here is the code:
PHP:
$release_date=strtotime($_ POST['rele ase_date'] );
$date=date('c', $release_date);
SQL:
$sql = "INSERT INTO books(title, description, price, release_date, quantity, active) VALUES ('$title','$description',' $price','$ date','$qu antity','$ active');" ;
Ray that article was spot on....using the date('c') function properly formatted the date, and Brian you were correct, I need to wrap the $date in single quotes in my SQL statement.
PHP:
$release_date=strtotime($_
$date=date('c', $release_date);
SQL:
$sql = "INSERT INTO books(title, description, price, release_date, quantity, active) VALUES ('$title','$description','
Ray that article was spot on....using the date('c') function properly formatted the date, and Brian you were correct, I need to wrap the $date in single quotes in my SQL statement.
I might look at it just a little differently (pidgin code):
PHP:
$release_date=strtotime($_ POST['rele ase_date'] );
if (!$release_date)
{
trigger_error('Invalid Date: ' . $_POST['release_date'], E_USER_WARNING); // TEST FOR FAILURE!
}
else
{
$date=date('c', $release_date);
}
PHP:
$release_date=strtotime($_
if (!$release_date)
{
trigger_error('Invalid Date: ' . $_POST['release_date'], E_USER_WARNING); // TEST FOR FAILURE!
}
else
{
$date=date('c', $release_date);
}
If the user must insert, you shou,d make sure the format of the date provided is correct.