删除行的安全方法

Safe way to delete rows

我正在编写代码以从 SQL 服务器数据库中删除基于 ID 的行。我想确保我的代码不会意外删除所有内容或删除不应该删除的内容。我目前的代码如下...

$st = $conn->prepare("if (select count(*) from sometable where id = :id)  = 1 
    delete from sometable where id = :id");

$st->bindParam(':id',$id);  

$st->execute();

这是一种安全的方法来删除单行而不意外删除所有内容吗?有更好的、已知的最佳实践方法吗?

编辑: 我在测试此代码时遇到错误 COUNT field incorrect or syntax error

我已经相应地更改了我的代码来解决这个问题(我希望我可以多次引用同一个字段并绑定一次但显然不是)

$st = $conn->prepare("if (select count(*) from sometable where id = ?)  = 1 
delete from sometable where id = ?");

$st->execute(array($id,$id));

(不妨使用我的问题来解决问题,因为 Whosebug 不会让我删除它)

Is this a safe way to delete a single row without accidentally deleting everything?

可以,只要您的 ID 对于每一行都是唯一的。

Is there a better, known best-practices way to do it?

没有