关于与SQL的连接,一切都很好。只是使用HTML5日期输入类型(包括下拉日历)数据和PHP似乎没有出现。 当我将数据输入SQL表时,“名称”显示但“日期”保持空白。 我在下面放了一些代码。 Form是input.html,数据处理代码是table.php。数据库名称是用户,表名是员工。
input.html
<html>
<form action='table.php' method='POST'>
Name <input type="text" name="name"> <br />
Date of Expiry <input type="date" name="date1"> <br />
<input type="submit" name="submit" value="Submit"> <br />
</form>
</html>
table.php
<?php
$name = $_POST["name"];
$date1 = $_POST["date1"];
$servername = "exampleserver.net";
$uname = "exampleusername";
$pass = "password";
$dbname = "users";
$errors = array();
$conn = new mysqli($servername, $uname, $pass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(mysqli_query($conn,"INSERT INTO staff (`name`, `date1`) VALUES('$name','$date1')")) {
header("Location: http://newpageafterdataentry.com");
die();
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
答案 0 :(得分:3)
在保存到DB mysql之前,您可以使用strtotime()转换为时间戳和date()进行格式化。请尝试以下操作:
$day1 = strtotime($_POST["date1"]);
$day1 = date('Y-m-d H:i:s', $day1); //now you can save in DB
完整代码应为:
<?php
$name = $_POST["name"];
$day1 = strtotime($_POST["date1"]);
$day1 = date('Y-m-d H:i:s', $day1); //now you can save in DB
$servername = "exampleserver.net";
$uname = "exampleusername";
$pass = "password";
$dbname = "users";
$errors = array();
$conn = new mysqli($servername, $uname, $pass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(mysqli_query($conn,"INSERT INTO staff (`name`, `date1`) VALUES('$name','$date1')")) {
header("Location: http://newpageafterdataentry.com");
die();
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>