【问题标题】:Deleting dates with PHP [duplicate]使用 PHP 删除日期 [重复]
【发布时间】:2015-06-04 14:21:54
【问题描述】:

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

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 部分中的变量更改为包含 '' 和 "" 但似乎没有任何效果?

【问题讨论】:

    标签: php mysql database date mysqli


    【解决方案1】:

    将日期放在引号中并从查询中删除多余的)

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

    【讨论】:

    • 易受 SQL 注入攻击
    • @Daan: 不,$date 是来自date 函数的日期,很明显。
    • 没注意到我的不好。
    猜你喜欢
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多