使用PHP删除日期

时间:2015-06-04 14:21:55

标签: php mysql database date mysqli

我正在尝试使用以下代码删除两个选定日期之间的所有日期。它显示提供的错误,而不是从数据库中删除任何内容,但我不知道为什么?如果有人看一看,我会很感激。

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部分中的变量以包含''和“”但似乎没有任何工作?

1 个答案:

答案 0 :(得分:0)

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

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