gpt4 book ai didi

php - 我正在尝试通过 PHP 将文本字符串转换为 mySQL 数据库的日期

转载 作者:太空宇宙 更新时间:2023-11-03 11:53:57 25 4
gpt4 key购买 nike

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="author" content="James McInnis">

<img src="../images/paper.jpg" class="bg">
<link href="../css/main.css" rel="stylesheet" type="text/css">
<ul class="nav">
<li><a href="../html/home.html">Home</a></li>
<li><a href="../html/person.html">Person</a></li>
<li><a href="../html/contacts.html">Contacts</a></li>
<li><a href="../html/events.html">Events</a></li>
<li><a href="../html/planner.html">Planner</a></li>
</ul>

</head>

<body>

<div id="content">
<p id=para2>Person</p>
<Center><p id=para1>Here is where you can add yourself or another person to the pool of available people. Once you're in the pool of available people, you can create a planner and add contacts to it. <br><br><br>ADD LINK TO QUERY PEOPLE HERE.<br><br><br></p>

<form action="../php/newConnect.php" method="post"><center>
Personal Identifier:<br>
<input type="text" name="Person_ID">
<br>
<center>First Name:<br>
<input type="text" name="First_Name">
<center> Last Name:<br>
<input type="text" name="Last_Name">
<center> Sex:<br>
<select name="Sex">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<!-- <input type="text" name="Sex"> -->
<center> Date of Birth:<br>
<input type="text" name="Date_Of_Birth">

<center><input type="submit">
</form>

</div>

</body>

</html>

我们使用的数据库将出生日期存储在日期类型中,格式为 YYYY-MM-DD。日期已发布到数据库,但它们都是 1970-01-01,我猜这是默认值。我是否错误地使用了 strtontime 或 strtodate 函数?或者我应该更改 HTML 端的输入类型吗?

<!DOCTYPE html>
<html>
<body>

<?php
$conn=mysqli_connect("localhost", "james", "QzVHdN", "james_PlannerDB");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if (isset($_POST['Person_ID']) && isset($_POST['First_Name']) && isset($_POST['Last_Name']) && isset($_POST['Sex']) && isset($_POST['Date_Of_Birth'])){

$Person_ID = $_POST["Person_ID"];
$First_Name = $_POST["First_Name"];
$Last_Name = $_POST["Last_Name"];
$Sex = $_POST["Sex"];
$Date_Of_Birth = $_POST[$Date_Of_Birth];
$Date_Of_Birth = date('Y-m-d',strtotime(str_replace('/','-',$Date_Of_Birth)));






$statement = "INSERT INTO PERSON(Person_ID, First_Name, Last_Name, Sex, Date_Of_Birth) VALUES ('" . $Person_ID . "','" . $First_Name . "' , '" . $Last_Name . "', '" . $Sex . "','" . $Date_Of_Birth . "')";

if ($conn->query($statement) === TRUE) {
echo 'Person added!';
} else {
echo 'Problem detected';
}
} else {
echo 'Missing parameters';

}
mysqli_close($conn);

?>

</body>
</html>

最佳答案

PHP 用 /- 处理日期。如果您使用 -,PHP 将以 DD-MM-YYYY 而非 MM/DD/YYYY 格式解释日期。因此,根据您的代码判断,您需要让所有输入都采用 DD-MM-YYYY 或 YYYY-MM-DD 格式,以便 strtotime() 正确解释它。

但是,Chris 似乎在您的代码到达此点之前就指出了它的错误。

关于php - 我正在尝试通过 PHP 将文本字符串转换为 mySQL 数据库的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34145520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com