In “form.php”
<html>
<body>
<form action="process.php" method="post">
<input type="text" name="dat" />
<input type="submit" value="submit" />
</form>
</body>
</html>
In “process.php”:
<?php
echo $_POST['dat'];
mysql_connect("localhost", "root", "123456")
or die("can't connect");
mysql_select_db("st")
or die("can't selectdb");
$query = "INSERT INTO sts (time) VALUES ( " . $_POST['dat']. " ) ";
$result = mysql_query($query)
or die(mysql_error());
$mysql_close();
?>
This give me 0000-00-00 in the database, in the field name “time” of type “DATE”.
If using this:
$time = explode("-", $_POST['dat']);
$query = "INSERT INTO sts (time) VALUES ( " . date("m-d-Y", mktime(0, 0, 0, $time[1], $time[2], $time[0])). " ) ";
-> doesn’t work either
EDIT:
I’ve changed from “m-d-Y” to “Y-m-d” for appropriate semantic, but it still doesn’t work
However, if I change the code like this:
$query = "INSERT INTO sts (time) VALUES ('1991-10-05') ";
it works.
Can anyone explain this for me?
Neither if your two PHP examples have the date being quoted. They’ll generate queries that look like:
which will be parsed down to
2009-01-02 is a numeric literal with two subtractions, NOT a date. To get get MySQL to treat this as a date value, it MUST be quoted:
So your PHP should look like: