0

I am trying to get this code below to delete all dates between two selected dates. It is showing the error provided and not deleting anything from the database but I am not sure why? If someone would take a look, I would be appreciative.

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();

}

I have tried changing the variable in the SQL section to include '' and "" but nothing seems to be working?

Aidanpt6
  • 93
  • 9

1 Answers1

0

Put date into quotes and remove extra ) from your query.

$sql = "DELETE FROM calendar WHERE date = '" . $date . "'";
pavel
  • 26,538
  • 10
  • 45
  • 61