Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Insert date to mysql database problem

I have simple php form where user input date format dd.mm.yyyy and then that info goes to action page which in turn uses a sql statement to send the info to a mysql table. One of the fields is a date field and  the field has 00.00.0000 in it? And my sql query shows only last inserted data? Only one row?

Every other data from a form goes nicely to database.
form (date input)
<input type="text" name="aika">
 
action page									  <?php
$host = "sql";
$username="x";
$password="x";
$database="x";
 
mysql_connect(sql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
 
$aika = strftime("%dd.%mm.%YYYY", strtotime($_POST['aika']));
$kaupunki=$_POST['kaupunki'];
$maa=$_POST['maa'];
$esiintymispaikka=$_POST['esiintymispaikka'];
 
$query = "INSERT INTO kiertue (aika, kaupunki, maa, esiintymispaikka) VALUES
('$aika','$kaupunki','$maa','$esiintymispaikka')" ;mysql_query($query);
 
mysql_close();
?>

Open in new window

0
tikkanen
Asked:
tikkanen
1 Solution
 
Beverley PortlockCommented:
To insert into a MySQL table's date field, the date must be in the format YYYY-MM-DD so try this (UNTESTED) code


$aika = strftime("%dd.%mm.%YYYY", strtotime($_POST['aika']));
$aika2 = date("Y-m-d",  strtotime($_POST['aika']) );

$kaupunki=$_POST['kaupunki'];
$maa=$_POST['maa'];
$esiintymispaikka=$_POST['esiintymispaikka'];
 
$query = "INSERT INTO kiertue (aika, kaupunki, maa, esiintymispaikka) VALUES
('$aika2','$kaupunki','$maa','$esiintymispaikka')" ;mysql_query($query);
 
0
 
tikkanenAuthor Commented:
Thanks!!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now