所以我尝试创建一个表单,将图像、描述和日期上传到数据库中。一切正常,但不管我怎么努力,日期都是0000-00-00,有什么想法吗?我大部分时间都在遵循一个教程,但它没有添加日期,我想这样做,因为以后我希望能够在提交这些条目的日期前调用它们。
<?php require('check.php');
require_once('settings.php');
include('header.php');
// Create database connection
$db = mysqli_connect("localhost", "-----", "-----", "----");
// Initialize message variable
$msg = "";
// If upload button is clicked ...
if (isset($_POST['upload'])) {
// Get image name
$image = $_FILES['image']['name'];
// Get text
$image_text = mysqli_real_escape_string($db, $_POST['image_text']);
// Get Date
$sermon_date = mysqli_real_escape_string($db, $_POST['sermon_date']);
// image file directory
$target = "images/sermons/".basename($image);
$sql = "INSERT INTO images (image, image_text, sermon_date) VALUES ('$image', '$image_text', '$sermon_date')";
// execute query
mysqli_query($db, $sql);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
$result = mysqli_query($db, "SELECT * FROM images");
?>
<h1>Update Sermon Series</h1>
<form method="POST" action="success.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
Sermon Graphic <br />
<input type="file" name="image"><br /><br />
<textarea
id="text"
cols="40"
rows="4"
name="image_text"
placeholder="Describe Sermon"></textarea><br />
Beginning Date of Sermon:<br />
<input type="date" name="sermon_date">
<button type="submit" name="upload">POST</button>
</form>
</body>
</html>
这里是我创建表的地方
$connect = new MySQLi($db_server, $db_user, $db_password, $db_database);
if ($connect->connect_error) {
die("Connection failed: " . $connect->connect_error);
}
$sql = "CREATE TABLE images (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
image VARCHAR(100) NOT NULL,
image_text LONGTEXT NOT NULL,
sermon_date DATE NOT NULL
)";
if ($connect->query($sql) === TRUE) {
echo "Table created successfully";
} else {
echo "Error creating table:" . $connect ->error;
}
1条答案
按热度按时间pcrecxhr1#
日期需要以格式保存到数据库中
YYYY-MM-DD
,html中的日期字段应该已经在做了。如果您的输入没有以这种格式发送日期(无论出于什么原因),您可以使用php将其转换为这种格式。下面的代码获取发布日期并将其转换为上面的格式(在php中,这转换为
Y-m-d
).请注意
strtotime
如果您接受的日期不是php可以检测到的标准格式,则可能不起作用。