sql delete查询成功,但数据仍然存在


The sql delete query is successful but the data still remain

我试图使用php删除数据库的记录,正如我在标题中所说的sql删除查询是成功的,但数据仍然存在。它引导我到hajj.php,无论如何,请帮助。

mysqli_query($connection,$sSQL) ;
$db = mysqli_select_db($connection, $database);
if(isset($_GET['id'])) {
    $id=$_GET['id'];
    $query1=mysqli_query($connection, "delete from hajj where id='$id'");
    if($query1) {
        header('location: hajj.php');
    } else {
        echo 'error';
    }
}

我很确定问题是你使用"'"在你的id。由于数据库使用整数而不是字符串,因此您不使用"'"。尝试将查询编辑为:

$query1 = mysqli_query($connection, "DELETE FROM hajj WHERE id=$id");

应该可以了。

另外,我看到你从url $_GET[' ID ']中获得ID,请记住,代码将是SQL注入的。您应该使用mysqli_real_escape_string来防止这种情况(在这里阅读:http://php.net/manual/en/mysqli.real-escape-string.php)

的例子:

$id = mysqli_real_escape_string($connection,$_GET['id']);

希望有帮助!

where子句中的id设置不正确,您需要正确连接$id变量,如下所示:

$query1=mysqli_query($connection, "delete from hajj where id='".$id."'");

您需要像这样连接$id:

"delete from hajj where id=".$id

解释:当写"string with '$id'加单引号"时:

'$id'被解释为字符串,因为它在" "里面。

但是,写:"string with ".$id:

将计算$id,然后将结果与字符串

连接起来。