gpt4 book ai didi

php - 使用 PHP 删除日期

转载 作者:行者123 更新时间:2023-11-29 22:21:43 25 4
gpt4 key购买 nike

我试图获取下面的代码来删除两个选定日期之间的所有日期。它显示提供的错误并且没有从数据库中删除任何内容,但我不确定为什么?如果有人愿意看一下,我将不胜感激。

if(isset($_POST['remove'])){

$servername = "localhost";
$username = "u779108225_admin";
$password = "password";
$dbname = "u779108225_main";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$date_from = $_POST['date_from'];
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp

// Specify the end date. This date can be any English textual format
$date_to = $_POST['date_to'];
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp

// Loop from the start date to end date and output all dates inbetween
for ($i=$date_from; $i<=$date_to; $i+=86400) {
$date = date("Y-m-d", $i);
echo $date;
$sql = "DELETE FROM calendar WHERE date = $date)";
if ($conn->query($sql) === TRUE) {
$complete = 'The dates have been removed from the database.';
} else {
$complete = 'An error has been detected, please try again later.';
}
}
echo $complete;
$conn->close();

}

我尝试更改 SQL 部分中的变量以包含“”和“”,但似乎没有任何效果?

最佳答案

将日期放入引号中并从查询中删除额外的 )

$sql = "DELETE FROM calendar WHERE date = '" . $date . "'";

关于php - 使用 PHP 删除日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30646447/

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